简体   繁体   English

如何在ng-model中使用ng-select

[英]How to use ng-select with ng-model

I am working on a code where I am using ng-repeat to iterate over options. 我正在使用ng-repeat遍历选项的代码上工作。 now I want to use ng-select to select some value with a given condition but as the AngularJS docs mention : 现在我想使用ng-select在给定条件下选择一些值,但是正如AngularJS文档所述:

ngSelected does not interact with the select and ngModel directives, it only sets the selected attribute on the element. ngSelected不会与select和ngModel指令交互,它仅在元素上设置selected属性。 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. 如果在选择上使用ngModel,则不应在选项上使用ngSelected,因为ngModel将设置选择值和所选选项。

but as the structure of the ng-repeat is like the given below: 但由于ng-repeat的结构如下所示:

  <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. 我不能在选择中使用ng-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. 现在,我不想遍历长列表,将值与条件进行比较,然后在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. 我认为使用ng-options可以解决问题,但是我对ng-model仍然了解的是,它在ng-model中设置了完整的对象,这是我不想要的,因为这样,我可能需要更改一个较大的对象。代码量。

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

add ng-model to your option line ng-model添加到您的选项行

you now have the data which you can access from your scope 您现在拥有可以从合并范围访问的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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