I am rendering key:value object array with ng-repeat like this:
我正在渲染关键:使用ng-repeat的值对象数组,如下所示:
<div ng-controller="mainCtrl">
<div ng-repeat="record in records">
<div ng-repeat="(key, value) in record">
<input ng-model="key" />: <input ng-model="value" />
</div>
</div>
</div>
JS:
JS:
var mainCtrl = function($scope){
$scope.records = [
{'key1':'val1'},
{'key2':'val2'}
];
}
Problem is that keys and values cannot be updated through input tags. For some reason it becomes one way binding after making ng-repeat iterate over (key,value).
问题是无法通过输入标签更新键和值。由于某种原因,在使用ng-repeat迭代(键,值)后,它成为单向绑定。
Fiddle: http://jsfiddle.net/BSbqU/1/
小提琴:http://jsfiddle.net/BSbqU/1/
How can I make it a two way binding? Or should I approach this problem in a different way then nested ng-repeat?
我怎样才能使它成双向绑定?或者我应该以不同的方式处理此问题然后嵌套ng-repeat?
3 个解决方案
#1
6
<div ng-controller="mainCtrl">
<div ng-repeat="record in records">
<input ng-model="record.name" />: <input ng-model="record.value" />
</div>
</div>
And the JS:
和JS:
var myApp = angular.module('myApp', []);
var mainCtrl = function($scope){
$scope.records = [
{'name':'key1','value':'val1'},
{'name':'key2', 'value':'val2'}
];
}
#2
6
This option works with object:
此选项适用于对象:
<div ng-controller="mainCtrl">
<div ng-repeat="record in records">
<div ng-repeat="(key, value) in record">
<input ng-model="key" />: <input ng-model="record[key]" />
</div>
</div>
</div>
Not a brilliant, but it works.
不是很棒,但它确实有效。
#3
0
After scratching my head to the bone, i find a way to update object key names. It is a bit twisted but it works for me.
在抓住我的头骨后,我找到了更新对象键名称的方法。它有点扭曲,但它对我有用。
replace
更换
<input ng-model="key" />
with
同
<input type="text" ng-model="key" ng-change="update_key(record,key,$index)" line-key/>
you will need the 'lineKey' directive just to keep focus on your input
你需要'lineKey'指令才能专注于你的输入
var app = angular.module('myApp',[]);
var focus_key=-1;
app.directive('lineKey', function () {
return function (scope, element, attrs) {
if(focus_key==scope[attrs.ngModel]){
focus_key=-1;
element[0].focus();
}
};
});
finally, add the 'update_key' method to your controller
最后,将“update_key”方法添加到控制器
app.controller('mainCtrl',['$scope'],function($scope){
$scope.records = [
{'key1':'val1'},
{'key2':'val2'}
];
$scope.update_key=function(obj,new_key,id){
var keys = Object.keys(obj);
var values = Object.values(obj);
var old_key = keys[id];
if(keys.indexOf(new_key)==-1&&new_key.length>0){
// clear ...
for(var i=0,key;key=keys[i];i++){ delete obj[key]; }
keys[id]=new_key;//... change key value ...
focus_key=new_key;//... notify to keep focus on modifyed input ...
// ... and refill to preserve the position in the object list
for(var i=0,key;key=keys[i];i++){ obj[key]=values[i]; }
}
};
}
#1
6
<div ng-controller="mainCtrl">
<div ng-repeat="record in records">
<input ng-model="record.name" />: <input ng-model="record.value" />
</div>
</div>
And the JS:
和JS:
var myApp = angular.module('myApp', []);
var mainCtrl = function($scope){
$scope.records = [
{'name':'key1','value':'val1'},
{'name':'key2', 'value':'val2'}
];
}
#2
6
This option works with object:
此选项适用于对象:
<div ng-controller="mainCtrl">
<div ng-repeat="record in records">
<div ng-repeat="(key, value) in record">
<input ng-model="key" />: <input ng-model="record[key]" />
</div>
</div>
</div>
Not a brilliant, but it works.
不是很棒,但它确实有效。
#3
0
After scratching my head to the bone, i find a way to update object key names. It is a bit twisted but it works for me.
在抓住我的头骨后,我找到了更新对象键名称的方法。它有点扭曲,但它对我有用。
replace
更换
<input ng-model="key" />
with
同
<input type="text" ng-model="key" ng-change="update_key(record,key,$index)" line-key/>
you will need the 'lineKey' directive just to keep focus on your input
你需要'lineKey'指令才能专注于你的输入
var app = angular.module('myApp',[]);
var focus_key=-1;
app.directive('lineKey', function () {
return function (scope, element, attrs) {
if(focus_key==scope[attrs.ngModel]){
focus_key=-1;
element[0].focus();
}
};
});
finally, add the 'update_key' method to your controller
最后,将“update_key”方法添加到控制器
app.controller('mainCtrl',['$scope'],function($scope){
$scope.records = [
{'key1':'val1'},
{'key2':'val2'}
];
$scope.update_key=function(obj,new_key,id){
var keys = Object.keys(obj);
var values = Object.values(obj);
var old_key = keys[id];
if(keys.indexOf(new_key)==-1&&new_key.length>0){
// clear ...
for(var i=0,key;key=keys[i];i++){ delete obj[key]; }
keys[id]=new_key;//... change key value ...
focus_key=new_key;//... notify to keep focus on modifyed input ...
// ... and refill to preserve the position in the object list
for(var i=0,key;key=keys[i];i++){ obj[key]=values[i]; }
}
};
}