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

标签 javascript angularjs angularjs-directive angular-ui angular-ui-bootstrap

我目前在我的项目中使用 angular-ui/ui-select。我能够毫无问题地将 ui-select 的值绑定(bind)到一个对象,但是它绑定(bind)了正在迭代的整个 item。我只想根据 item.codeId 进行绑定(bind),这样我就可以保留正确的数据,并在加载页面时在下拉列表中显示正确的值。

如何设置 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= 子句并去掉 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>

关于javascript - 如何通过 ui-select 值设置 ng-model 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25065321/

相关文章:

javascript - AngularJS 在 Angular 替换它之前获取指令 HTML 并在代码中使用它

javascript - 如何将用户选择的酒店房间乘以入住天数?

javascript - slack chat.postMessage API 端点不允许授权 header

javascript - 我收到此错误 "No module: ngResource"

javascript - Chrome 打包应用无法读取 urlIsSameOrigin 处未定义的属性 'protocol'

javascript - 新的自定义 Angular Directive(指令)不起作用

javascript - 如何让这个特定的animatedModal.js加载到document.ready上?

javascript - Bootstrap-multiselect 非常慢,有大量选项

javascript - AngularJS ng-repeat 唯一的对象数组

angularjs - 'watch'属性是否可以改变