[英]Angular ng-options to ng-repeat with extra options
I have a <select>
element and I use ng-options
to add insert available values, my code is like 我有一个<select>
元素,并且我使用ng-options
添加插入可用值,我的代码就像
<select ng-model="selectedCollection"
ng-options="collection.collectionName for collection in seriesCollection">
</select>
This works fine, and in $scope.selectedCollection
I have the whole collection
object. 这工作正常,在$scope.selectedCollection
我拥有整个collection
对象。
Now I need to add some extra <option>
elements based on some parameters. 现在,我需要基于一些参数添加一些额外的<option>
元素。 I think the best way is avoid using ng-options
and do something like this 我认为最好的方法是避免使用ng-options
并执行类似的操作
<select ng-model="selectedCollection"
<option value='c1' ng-if="case1">somevalue</option>
<option value='c2' ng-if="case2">somevalue2</option>
<option ng-repeat="collection in seriesCollection">
{{ collection.collectionName }}
</option>
<option value='c3' ng-if="case3">somevalue3</option>
</select>
This way selectedCollection
contains a string named c1, c2 or c3 when a "static" option is selected, but I would like it to contain the whole collection
object when one of them is selected. 这样,当选择了“静态”选项时, selectedCollection
包含名为c1,c2或c3的字符串,但是当希望selectedCollection
其中一个时,我希望它包含整个collection
对象。
I tried with <option ng-repeat="collection in seriesCollection" value="{{ collection }}">
but then the stored value is not the collection
object but a string representing it's serialization. 我尝试使用<option ng-repeat="collection in seriesCollection" value="{{ collection }}">
但随后存储的值不是collection
对象,而是代表其序列化的字符串。 How can I do? 我能怎么做?
Unfortunately what you try to achieve can't be done. 不幸的是,您尝试实现的目标无法完成。 It is the ngOptions
directive that takes care of binding the object to model and any other method like ngRepeat
will set model as string therefore value can't be object and mixing ngOptions
with html option
will not work. ngOptions
指令负责将对象绑定到模型,其他任何方法(例如ngRepeat
都会将模型设置为字符串,因此值不能是对象,并且将ngOptions
与html option
混合将不起作用。
You can utilise ngChange
or add the cases to the collections and use filter to return only the one that should be displayed 您可以利用ngChange
或将案例添加到集合中,并使用过滤器仅返回应显示的案例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.