首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过ui-select值设置ng-model值

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

Stack Overflow用户
提问于 2014-08-01 01:27:59
回答 2查看 20.4K关注 0票数 15

我目前在项目中使用angular-ui/ui-select。我可以毫无问题地将ui-select的值绑定到对象,但是它绑定的是正在迭代的整个item。我只想基于item.codeId绑定,这将允许我持久化正确的数据,并在页面加载时在下拉列表中显示正确的值。

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

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-01 11:31:10

您的代码没有问题,但是在使用子数组(constants.states)时有一个bug导致了这种情况。

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

新版本的v0.5.1发布。如果您使用的是bower,只需运行bower update即可。

票数 8
EN

Stack Overflow用户

发布于 2014-08-01 01:51:58

我相信您所要做的就是使用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抄袭过来的

代码语言:javascript
复制
<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>
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25065321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档