简体   繁体   中英

How to use ng-select with ng-model

I am working on a code where I am using ng-repeat to iterate over options. now I want to use ng-select to select some value with a given condition but as the AngularJS docs mention :

ngSelected does not interact with the select and ngModel directives, it only sets the selected attribute on the element. If you are using ngModel on the select, you should not use ngSelected on the options, as ngModel will set the select value and selected options.

but as the structure of the ng-repeat is like the given below:

  <select name="selectedPerson" id="selectedPerson" class="form-control"  ng-model="selectedPerson" >
   <option value="-1">Select Person</option>
   <option ng-repeat="name in nameList" value="{{name.id}}" >{{name.name}}</option>
 </select>

I can not use ng-select in the select. Now I do not want to iterate over a long list and compare the value with the condition and then set it in ng-model. I think the problem may get solved with using ng-options, but what I still know about ng-model is that it set complete object in ng-model, which I do not want, as because of that I may need to change a large amount of code.

<option ng-model="value" ng-repeat="name in nameList" value="{{name.id}}" >{{name.name}}</option>

add ng-model to your option line

you now have the data which you can access from your scope

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM