如何通过ui-select值设置ng-model值

zma*_*anc 15 javascript angularjs angular-ui angularjs-directive angular-ui-bootstrap

我目前在我的项目中使用angular-ui/ui-select.我能够将ui-select的值绑定到一个没有问题的对象,但是它绑定了item正在迭代的整个.我想只基于item.codeId这个绑定将允许我保持正确的数据,并在页面加载时在下拉列表中显示正确的值.

如何设置ui-select来执行此操作?

<ui-select ng-model="myObject.stateCode" id="stateCode">
    <ui-select-match placeholder="Select a state...">{{$select.selected.codeDescription}}</ui-select-match>
    <ui-select-choices repeat="item in constants.states | filter: $select.search" value="{{$select.selected.codeId}}">
        <div ng-bind-html="item.codeDescription | highlight: $select.search"></div>
        <small ng-bind-html="item.codeId | highlight: $select.search"></small>
    </ui-select-choices>
</ui-select>
Run Code Online (Sandbox Code Playgroud)

urb*_*ons 16

我相信你会做的是使用该repeat=子句并摆脱value属性.这里有一个例子:http: //plnkr.co/edit/htm8UNxVOlC076LVVezE?p=preview

我从中复制过:https: //github.com/angular-ui/ui-select/blob/master/examples/demo-bind-to-single-property.html

<ui-select ng-model="myObject.stateCode" id="stateCode">
    <ui-select-match placeholder="Select a state...">{{$select.selected.codeDescription}}</ui-select-match>
    <ui-select-choices repeat="item.codeId as item in constants.states | filter: $select.search">
        <div ng-bind-html="item.codeDescription | highlight: $select.search"></div>
        <small ng-bind-html="item.codeId | highlight: $select.search"></small>
    </ui-select-choices>
</ui-select>
Run Code Online (Sandbox Code Playgroud)

  • @Marks [这里有一个例子](https://github.com/angular-ui/ui-select/blob/master/examples/demo-bind-to-single-property.html).`ui-select`几乎没有文档,你必须从示例中推断它. (2认同)

dim*_*irc 8

你的代码很好,但是在使用子数组(constants.states)时有一个导致这种情况的错误.

我刚刚在https://github.com/angular-ui/ui-select/pull/131解决了这个问题,特别是这个提交

新版本v0.5.1发布.如果您正在使用凉亭,请运行bower update.