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

时间:2018-06-15 09:59:34

标签: angularjs ui-select angular-ui-select

我有这种格式的对象数组

$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列设置ui-select值,但我无法这样做。我确信我错了,这个插件也很新。请帮帮我。

1 个答案:

答案 0 :(得分:2)

使用以下内容更改ng-repeat:

<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