代码之家  ›  专栏  ›  技术社区  ›  Steve

使用对象数组中的id设置ui select值

  •  2
  • Steve  · 技术社区  · 6 年前

    好吧,我有这个格式的对象数组

    $scope.itemArray = [
            {id: 1, name: 'first'},
            {id: 2, name: 'second'},
            {id: 3, name: 'third'},
            {id: 4, name: 'fourth'},
            {id: 5, name: 'fifth'},
        ];
    
    $scope.selectedItem = $scope.itemArray[0];//this works fine
    $scope.selectedItem = $scope.itemArray[0].id;//this doesn't works
    


    这是html部分:

    <ui-select ng-model="selectedItem">
        <ui-select-match>
            <span ng-bind="$select.selected.name"></span>
        </ui-select-match>
        <ui-select-choices repeat="item in itemArray | filter: $select.search">
            <span ng-bind="item.name"></span>
        </ui-select-choices>
      </ui-select>
    

    我要做的是使用 id 但是我不能这么做。我确信我在某个地方是错的,而且对这个插件来说也是非常新的。请帮帮我。

    1 回复  |  直到 6 年前
        1
  •  3
  •   NTP    6 年前

    用以下方法更改您的ng重复:

    <ui-select ng-model="selectedItem">
        <ui-select-match>
            <span ng-bind="$select.selected.name"></span>
        </ui-select-match>
        <ui-select-choices repeat="item.id as item in itemArray | filter: $select.search">
            <span ng-bind="item.name"></span>
        </ui-select-choices>
    </ui-select>
    

    Demo

    您可以在这个git页面中找到关于这个插件的更多信息。 https://github.com/angular-ui/ui-select/wiki/ui-select-choices

    推荐文章