【问题标题】:Get index of selected choice in ui-select - Angularjs在 ui-select 中获取所选选项的索引 - Angularjs
【发布时间】:2015-06-27 18:51:00
【问题描述】:

我使用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 数组中搜索值,但这对我来说还不够好,因为有可能会有多个具有相同值的项目...

有什么想法吗?

谢谢:)

【问题讨论】:

    标签: javascript angularjs ui-select


    【解决方案1】:

    你有错误的重复表达式。

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

    你是在告诉 ui-select,迭代 trought places 并在模型中绑定 item.name,把它改成

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

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

    【讨论】:

    • 如果你想要索引你只需要调用 $scope.places.indexOf($scope.sender) ,它将返回原始数组中的索引
    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 2015-09-26
    • 2023-03-15
    • 2017-10-28
    • 1970-01-01
    • 2012-11-13
    • 2011-05-07
    相关资源
    最近更新 更多