我想将颜色列动态添加到“解决方案”表。我的代码基于angularjs和json。我试过如下:angularjs向json数据添加一列
$scope.solutions.push({"color": value2.color});
的value2.color是动态生成foreach循环决定,但我不能添加新列,难道我错了吗?这里是Plunker网址:http://plnkr.co/edit/5gK5cBRdLQ6s8Gyy6ad8?p=preview
谢谢!
我想将颜色列动态添加到“解决方案”表。我的代码基于angularjs和json。我试过如下:angularjs向json数据添加一列
$scope.solutions.push({"color": value2.color});
的value2.color是动态生成foreach循环决定,但我不能添加新列,难道我错了吗?这里是Plunker网址:http://plnkr.co/edit/5gK5cBRdLQ6s8Gyy6ad8?p=preview
谢谢!
推对象数组相反的,你需要得到该索引的对象,并添加新的特性。
angular.forEach($scope.solutions, function(value1, i) {
angular.forEach($scope.eliminations, function(value2, j) {
if (value1.alien === value2.alien && value1.world === value2.world){
$scope.solutions[i].color = value2.color;
}
});
});
由于我不能调用你的解决方案或消除数据结构,所以很难编辑你的plunker。但随着拉梅什拉金德伦上面所说的,.push只能用于初始化数组
你需要在你的范围和
$scope.solutionsArray = [];
,而不是推到$ scope.solutions创建一个数组,你需要推到$ scope.solutionsArray。
您还需要通过添加soultionsArray到您的NG-reapeat
<tr ng-repeat="solution in solutionsArray | filter:SelectedStudent.student_name | filter: {student_name: ''}" >
这是我能做的最好的,没有看到你的数据结构来更新您的视图。我希望这有帮助! goood运气
嗨,亚伦,谢谢你的回复!我更新了plunker,我相信$ scope.solutions是一个json数组。我想将消除阵列中的颜色填充到解决方案中。 – Lisa 2015-03-19 15:56:07
你可能要更新你的代码是这样的:
angular.forEach($scope.solutions, function(value1, key1) {
angular.forEach($scope.eliminations, function(value2, key2){
if(value1.alien === value2.alien && value1.world === value2.world){
console.log(value2.color);
// value1 is a solution object in the solutions array
value1["color"] = value2.color;
}
});
});
正确...但我没有解决方案阵列中的“颜色”键,所以它没有工作......谢谢 – Lisa 2015-03-19 16:08:38
它是正确的。它显示任何错误? – vamsikrishnamannem 2015-03-19 04:40:02
你能发布一个链接到你的完整的JavaScript源?也许是一个[plunker](http://plnkr.co) – bluetoft 2015-03-19 04:43:41
是你称它为value2的原因,它是foreach提供的第二个参数?第二个参数是关键,请尝试value1.color。只是一个想法。 – Aaron 2015-03-19 04:50:54