javascript - AngularJS - ng-repeat,更新尚未定义的模型对象索引

标签 javascript angularjs

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
    }
}]);

http://jsfiddle.net/j82mzfj9/

关于javascript - AngularJS - ng-repeat,更新尚未定义的模型对象索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27944617/

相关文章:

javascript - ng.core.Component(...).Class 不是函数

javascript - 通过 javascript 将源应用到 <img> 标记时出现问题

javascript - 添加两个链表并在javascript中的新链表讨论中输出结果

javascript - 输入/返回时指令语法错误

java - 使用 Spring Security 后 $http 请求给出 403?

javascript - Angular js中点击刷新按钮时无法清除详细信息?

javascript - 在 Angular 应用程序中加载 Highcharts

javascript - jQuery 从跨度内选择文本

javascript - 我该如何修复 - 未捕获的类型错误 : Cannot read property 'scrollHeight' of undefined

javascript - 在 ionic 应用程序中发送邮件