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

Ste*_*eve 2 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
Run Code Online (Sandbox Code Playgroud)


这是 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>
Run Code Online (Sandbox Code Playgroud)

我想要做的是使用id列设置 ui-select 值,但我无法这样做。我确信我在某个地方错了,而且对这个插件也很陌生。请帮我。

NTP*_*NTP 5

使用以下内容更改 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>
Run Code Online (Sandbox Code Playgroud)

演示

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