这是我的代码如何在ng-repeat中为AngularJs中的mutilpe文本字段实现ng模型?
function Ctrl($scope) {
$scope.data = [];
$scope.data = [{
"label": "name",
"type": "string"
}, {
"label": "email",
"type": "string"
}];
$scope.addFields = function() {
var count = 0;
angular.forEach($scope.data, function(obj) {
if (count == 2) {
return true;
}
$scope.data.push(obj);
count = count + 1;
});
};
$scope.save = function() {
console.log($scope.data);
};
}
<div ng-app>
<div ng-controller="Ctrl">
<input type="button" value="add" ng-click="addFields()" />
<div ng-repeat="eachItem in data">
<label>{{eachItem.label}}</label>
<input type="text" ng-model="eachItem.value" />
</div>
<input type="button" value="save" ng-click="save()" />
</div>
</div>
这是我的jsfiddle http://jsfiddle.net/0c5p38dt/5/ 在上面的代码我有多个对象的阵列,这些目的从服务动态获取。当我再次单击添加按钮时,相同的对象将推到相同的数组。我在ng-repeat内部使用ng-model作为textfield。但是当我输入的数据也会影响其他文本域。所以如何区分数组中多个相同对象的ng模型值。
如你所说,同样的对象推到相同的阵列。输入数据时,您正在修改相同的对象,因此绑定到相同ng模型的所有字段也会被更改。在addField函数中,如果克隆该对象,即angular.copy(obj)。在将其推入阵列之前,您应该看到区别。但我不确定这是否是你想要的。 – sdfacre