I have a data object "data" as below, but i need to iterate them over the other array called "values", and when I update the values in view inputs, i need to update the model object "data" accordingly,
但问题是索引“b”没有在“数据”对象中定义为索引“a”,所以我如何更新 Controller 中的“数据”对象,对于包括“b”在内的所有“值”数组索引尚未在“数据”对象中定义。 对于这种情况有没有其他方法?
Controller
$scope.values=["a","b"];
$scope.data={"a":{name:"A"}};
$scope.updateRate = function(val) {
$scope.data[val]=//i want the input value here;
};
查看
<tr ng-repeat="v in values">
<td><input ng-model="data[v].name" type="text" ng-blur="updateRate(v)"></td>
</tr>
最佳答案
为什么不在尝试设置模型之前在 Controller 中创建所有 key 对:
.controller('myCtrl', ['$scope', function($scope){
$scope.values = ["a","b"];
$scope.data = {};
for(var i=0;i<$scope.values.length;i++){
$scope.data[$scope.values[i]] = {};
}
$scope.updateRate = function(val){
$scope.data[val] = //not sure what your wanting to set it to here as the model is already the input value
}
}]);
关于javascript - AngularJS - ng-repeat,更新尚未定义的模型对象索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27944617/