[英]AngularJs Select2 multiple: ng-selected does not work
I'm using the following piece of code to select item if it is present in filters 我正在使用下面的代码来选择项目,如果它存在于过滤器中
<div ng-repeat="category in categories.data" ng-model="div1">
<div ng-repeat="(key, value) in category" mg-model="div1.div2">
{{ key + ":"}}
<select id={{key}} class="my_select"
data-ng-model="CategoryOption"
data-ng-change="updateCategories()"
data-ui-select2="{}" multiple >
<option ng-repeat="c in value"
ng-selected="(filters[key].length>0) && (filters[key].indexOf(c.trim()) !== -1)" >
{{c.trim()}}</option>
</select>
</div>
</div>
But it actually doesn't select anything... Other option is to set ng-model to filters.key, but selecting one element will cancel out the selection in another select, because they are bind to the same model... 但是实际上它什么也没选择。另一个选项是将ng-model设置为filters.key,但是选择一个元素将取消另一个选择的选择,因为它们绑定到相同的模型...
Given my setup from above, how can I restore my selection, using select2 multiple ? 从上面给出我的设置,如何使用select2 multiple恢复我的选择?
The only solution I found for my problem was to modify select2.js once again. 我发现的唯一解决方案是再次修改select2.js。
I used to pass a custom argument: 我曾经传递一个自定义参数:
<select id={{key}} class="my_select"
ng-model="select2"
ui-select2="{mySelection: filters[key] }" multiple >
<option ng-repeat="c in value track by $index"
value="{{c.trim()}}">{{c.trim()}}</option>
</select>
and in select2 I applied following changes: 在select2中,我应用了以下更改:
if(opts.mySelection && typeof opts.mySelection != undefined) {
elm.val(opts.mySelection).trigger("change");
}
just before: 之前:
controller.$render();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.