动态NG-模型输入NG-重复不工作 [英] Ng-repeat with dynamic ng-model on input not working
本文介绍了动态NG-模型输入NG-重复不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在控制器我有一个像这样的列表:
In controller I've a list like this:
scope.data = [ { user: { address: { city: 'Boston'} } } ];
和属性,在那里我已经名访问对象:
And a property, where I've the name to access the object:
scope.propertyName = 'user.address.city';
在HTML中,我有一个NG重复,我把一个动态输入编辑该值。
In HTML, I've a ng-repeat where I put a dynamic input to edit that value.
<div ng-repeat="item in data">
<input ng-model="item[propertyName]">
</div>
我的问题是:我如何可以将绑定在NG-模型项的值
My question is: How can I bind the value of the item with the ng-model?
推荐答案
您可以创建一个指令与编译,以间接设置NG-模型:
You can create a directive with compile, to set ng-model indirectly:
compile: function(el, attrs) {
return function(scope, el) {
el.attr('ng-model', attrs.ngModelItem + '.' + scope[attrs.ngModelRef]);
$compile(el)(scope);
};
}
请看看我的样本上jsbin:
please look my sample on jsbin:
http://jsbin.com/xizucu/edit?html,js,output
这篇关于动态NG-模型输入NG-重复不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文