angular-bootstrap 手风琴数据绑定问题 [英] angular-bootstrap accordion databinding issue
问题描述
我有 2 个相同型号的下拉菜单,一个在手风琴里面,另一个在外面.外部下拉菜单在 2 路数据绑定方面工作正常,但手风琴内部的下拉菜单似乎只有 1 路绑定,换句话说,在 UI 中选择不是设置模型值.我发现了一个建议 这里 使用 ng-change
将解决这个问题.它针对 修复,但不适用于
.想知道这可能是 angular-ui 中的错误.有人可以帮助解决这个问题.提前致谢!
室外手风琴
<label class="col-md-2 control-label" for="category">Category</label><div class="col-md-3"><select id="category" ng-model="category" name="category" type="text" class="form-control"><option ng-repeat="config.categories.sort() 中的category" value="{{category}}">{{category}}</option></选择>
内部手风琴
<手风琴组><div class="form-group"><label class="col-md-2 control-label" for="category">Category</label><div class="col-md-3"><select id="category" ng-model="category" ng-change="setCategory(category)" name="category" type="text" class="form-control"><option ng-repeat="config.categories.sort() 中的category" value="{{category}}">{{category}}</option></选择>
</accordion-group></手风琴>
我的模型 categories
是一个字符串数组:
示例:
类别":["管理 API","管理员许可证","adminGUI","天线房","应用程序生成器","备份/恢复",基础"]
ng-change 函数
$scope.setCategory = function(category) {$scope.category = 类别;};
直接绑定到作用域的一个属性几乎是一个坏主意,因为很多指令定义了自己的作用域,如果你的小部件(选择框)在里面这样的指令,您实际上将绑定到子范围的属性,而不是绑定到控制器范围的属性.
因此经验法则是:在你的 ng-model 中总是有一个点.表单控件应该修改您作用域的对象,而不是直接修改您的作用域.
在您的控制器中:
$scope.selection = {};
在您看来:
另外,请注意您使用相同的 ID 来标识两个选择框.这将使您的 DOM 无效.ID 唯一标识一个元素.
I have 2 drop downs with same models, one inside accordion and another outside.
The outside drop down works fine in terms of 2-way databinding but the one inside the accordion seems to have only 1-way binding, in other words selecting in the UI is not setting the model value. I found a suggestion here that using ng-change
will fix this problem. It fixed for <textarea>
but not for <select>
.
Wondering this could be bug in angular-ui. Can someone help on this issue. Thanks in advance!
outside accordion
<div class="form-group">
<label class="col-md-2 control-label" for="category">Category</label>
<div class="col-md-3">
<select id="category" ng-model="category" name="category" type="text" class="form-control">
<option ng-repeat="category in config.categories.sort()" value="{{category}}">
{{category}}</option>
</select>
</div>
</div>
Inside accordion
<accordion close-others="false">
<accordion-group>
<div class="form-group">
<label class="col-md-2 control-label" for="category">Category</label>
<div class="col-md-3">
<select id="category" ng-model="category" ng-change="setCategory(category)" name="category" type="text" class="form-control">
<option ng-repeat="category in config.categories.sort()" value="{{category}}">
{{category}}</option>
</select>
</div>
</div>
</accordion-group>
</accordion>
my model categories
is an array of string:
example:
"categories": [
"Admin API",
"Admin License",
"adminGUI",
"antennahouse",
"App Builder",
"Backup/Restore",
"Basis"]
ng-change function
$scope.setCategory = function(category) {
$scope.category = category;
};
Binding to an attribute of the scope directly is almost a bad idea, because many directives define their own scope, and if your widget (the select box) is inside such a directive, you will in fact bind to an attribute of the child scope rather than binding to an attribute of the controller scope.
The rule of thumbs is thus: always have a dot in your ng-model. The form controls should modify an object of your scope, and not your scope directly.
In your controller:
$scope.selection = {};
In your view:
<select ng-model="selection.category" ...>
Also, note that you're using the same ID to identify two select boxes. That will make your DOM invalid. An ID identifies an element uniquely.
这篇关于angular-bootstrap 手风琴数据绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!