如何通过ui-select值设置ng-model值
how to make ng-model value set by ui-select value
我目前在我的项目中使用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
。
相关文章:
- 从 Javascript 中设置 ng-model name
- 为什么 AngularJS 在使用 ng-model 时不会在文本框上设置长度属性
- AngularJS ng-model 未设置正确的值
- AngularJS更新ng-model的值以设置默认值
- ng-model 没有为我设置默认值
- 在Angularjs中,嵌套在指令中的控制器可以设置指令的ng-model
- 无法在选择下拉菜单中使用动态生成下拉菜单ng-model设置默认选项
- 在ng-model被设置为select>之前,Ng-change执行
- 角度JS:无法在ng-repeat中将值设置为ng-model变量
- 如何通过ui-select值设置ng-model值
- AngularJS -如何设置不同于ng-model的输入值
- Angularjs的输入[radio]带有唯一的ng-model,不能取消设置
- 如何在指令中设置ng-model的正确作用域?
- 在ng-options中设置ng-model的初始值
- 不能为ng-model的select设置默认值
- ng-model将JavaScript对象属性设置为array
- 在AngularJS中动态设置ng-model value为js变量的值
- 通过javascript为ng-model设置值
- 在ng-repeat中设置ng-model无效
- 如何动态设置ng-model