search

Home  >  Q&A  >  body text

angular.js - Angularjs select How to set an option to clear the selection

I want to have an option whose value is null. When I select it, the content in the input can be cleared.
My html is written like this

<select 
        ng-options="x as (x.name+' '+x.id) for x in items| orderBy:'name'"
        ng-model="selectedItem" ng-change="update()">
        <option value="">---Please select---</option>
</select>

<input type="text" ng-model="name" name="name" class="space" placeholder="Name" disabled="true">
<input type="text" ng-model="id" name="suburb" class="space" placeholder="id" disabled="true">

My js is

update(){
    $scope.name=$scope.selectedItem.name;
    $scope.id=$scope.selectedItem.id;
}

After I select an option, the content of the two inputs below will change according to the option. But when I click please select, the previous content is still in the two inputs and is not cleared. I tried writing select as

<select class="form-control" ng-model="selectedItem" ng-change="update()">

                    <option value="">none</option>
                    <option value="1">1</option>
                    <option value="2">2</option>

</select>

The option none can play this role. . . What is the reason for this? How can I use angularjs's select to implement this function?

漂亮男人漂亮男人2757 days ago523

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-15 17:08:05

    <input type="text" ng-model="selectedItem.name" name="name" class="space" placeholder="Name" disabled="true">
    <input type="text" ng-model="selectedItem.id" name="suburb" class="space" placeholder="id" disabled="true">
    
    

    reply
    0
  • Cancelreply