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

how to make ng-model value set by ui-select value

本文关键字:设置 ng-model ui-select 何通过      更新时间:2023-09-26

我目前在我的项目中使用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>

我相信你要做的是使用repeat=子句并摆脱值属性。下面是一个例子: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>

您的代码很好,但是在使用子数组(constants.states)时存在导致此问题的错误。

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

新版本v0.5.1发布。如果您正在使用bower,只需运行bower update