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

Get index of selected choice in ui-select - Angularjs

本文关键字:选项 索引 获取 ui select-Angularjs      更新时间:2023-09-26

我将angularjsui-select一起使用(https://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数组中搜索值,但这对我来说不够好,因为可能会有几个项目具有相同的值。。。

知道吗?

感谢:)

您有错误的repeat-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-place并在模型内绑定item.name,将其更改为

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

它将把完整的项对象绑定到ngModel,这样您就可以从位置数组中获得原始项。