Angular UI Select,预选数组的唯一值

时间:2015-07-06 11:29:26

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

假设我想要编辑一个现有实体,该实体具有一系列值,这些值也是可选值的一部分。 e.g。

var preSelectedLanguages = [
    {id: 2, iso: "de"},
    {id: 3, iso: "fr"}
]

var languages = [
    {id: 1, iso: "en"},
    {id: 2, iso: "de"},
    {id: 3, iso: "fr"},
    {id: 4, iso: "it"},
    {id: 5, iso: "us"}
]

我的ui-select指令类似于:

<ui-select multiple ng-model="preSelectedLanguages">
    <ui-select-match placeholder="Select language...">
        {{$item.iso}}
    </ui-select-match>
    <ui-select-choices repeat="l in languages track by language.id">
        {{language.iso}}
    </ui-select-choices>
</ui-select>

但是ui-select-choices选项列表似乎无法删除重复项,即使我使用了track by language.id

知道如何正确地做到这一点吗?

1 个答案:

答案 0 :(得分:2)

以这种方式对我有用

<ui-select multiple ng-model="preSelectedLanguages">
    <ui-select-match placeholder="Select language...">
        {{$item.iso}}
    </ui-select-match>
    <ui-select-choices repeat="l in languages track by l.id">
        {{l.iso}}
    </ui-select-choices>
</ui-select>