我有一个带有以下标记的 select2 下拉列表:
<select id="selectByName" ui-select2="select2Options" ng-model="selectId" data-placeholder="Select item by name" style="width:250px">
<option></option>
<option ng-repeat='item in items' data-show="{{item.show}}" value="{{item.id}}">
{{item.name}}
</option>
</select>
js包含以下内容:
$scope.items
(具有 id、布尔显示属性和名称属性的数组)
和选择2个选项:
select2Options : {
allowClear: true,
placeholder:"select a value",
formatResult: function(state) {
var $elem = angular.element(state.element),
isVisible = $elem.data('show');
return isVisible ? '<span style="color:red">'+state.text+'</span>':
<span style="color:blue">'+state.text+'</span>';
}
},
好吧,ng-repeat
正确更新了 html 标记并将 data-show 属性设置为 true 或 false,但该formatResult
函数不会更新此值。在html源代码data-show="true"
和formatResult
函数$elem.data('show') = false;
中,为什么每次打开select2时调用函数时它不更新?
这是一个说明我的问题的 plunker:plnkr.co/edit/d0LxuhzdQh7hMdzOoxpr?p=preview 。它看起来 formatResult 在第一次打开 select2 之前只正确更新了一次结果。