我目前在项目中使用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>发布于 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即可。
发布于 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抄袭过来的
<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>https://stackoverflow.com/questions/25065321
复制相似问题