在ui-select中获取所选选项的索引 - Angularjs

时间:2015-06-27 10:45:21

标签: javascript angularjs ui-select

我将angularjsui-selecthttps://github.com/angular-ui/ui-select/

一起使用

我有这段代码:

<ui-select tagging="addTagging" tagging-tokens="ENTER" ng-change="sourceChanged()" ng-model="sender" theme="bootstrap" sortable="true" ng-disabled="disabled">
    <ui-select-match>{{$select.selected.name}}</ui-select-match>
    <ui-select-choices data-id="$index" repeat="item.name as item in places | filter:$select.search">
        {{item.name}}
    </ui-select-choices>
</ui-select>

sourceChanged函数内部我想知道所选项目的索引..现在我只有值(scope.sender)..
我可以搜索places数组中的值,但这对我来说不够好,因为有可能会有几个具有相同值的项目......

有什么想法吗?

谢谢:)

1 个答案:

答案 0 :(得分:2)

你错了重复expr。

<ui-select-choices data-id="$index" repeat="item.name as item in places | filter:$select.search">
        {{item.name}}
    </ui-select-choices>

你正在告诉ui-select,迭代trought的地方并将item.name绑定到模型中,将其更改为

<ui-select-choices data-id="$index" repeat="item in places | filter:$select.search">
        {{item.name}}
    </ui-select-choices>

它会将完整的项目对象绑定到ngModel,因此您可以从场所数组中获得原始项目。