我注意到 Angular-UI 已经停止使用他们的 UI-Select2 指令,转而使用新的 UI-Select(具有多个主题 - select2、bootstrap、selectize)。
它看起来像这样:
<ui-select multiple ng-model="multipleDemo.colors" theme="select2" ng-disabled="disabled" style="width: 300px;">
<ui-select-match placeholder="Select colors...">{{$item}}</ui-select-match>
<ui-select-choices repeat="color in availableColors | filter:$select.search">
{{color}}
</ui-select-choices>
</ui-select>
<p>Selected: {{multipleDemo.colors}}</p>
最初,我的选择框应该是空的,但准备好接受键入的字符,即至少 4 个字符的字符串,然后进行 API 调用以检索要填充框的建议值列表。然后将选择一个值,并根据需要重复搜索。
首先我尝试$watch
了ng-model
在这种情况下是multipleDemo.colors
(使用演示中的这个例子)。API 调用从未发生过,然后我意识到了原因。实际模型根本没有改变,因为它只在进行选择时才会改变(我的选择框是空的,所以什么都不能选择)。
我的结论是我应该(能够)$watch
作为过滤器添加的内容,即filter: $select.search
.
有谁知道我应该如何在我的控制器中使用那个?
这个:
$scope.$watch('$select.search', function(newVal){
alert(newVal);
});
不起作用。
编辑:供任何人参考,请参阅这个简短的讨论:是否可以添加对自定义查询功能的支持,如 select2?
编辑2:
我通过$emit
在指令中使用解决了这个问题,因此该值现在可以在我的控制器中使用。但是现在我想知道如何覆盖指令的这一部分,以便指令本身可以保持不变,这样它就不会在未来的更新中中断?