我的范围如下
$scope.phones = [
{
id: 4,
name: "nokia",
accessories: [
{id: 1, name: "headset"},
{id: 3, name: "keyboard"},
{id: 5, name: "charger"}
]
},
{
id: 5,
name: "samsung",
accessories: [
{id: 5, name: "charger"}
]
},
{
id: 6,
name: "iphone",
accessories: [
{id: 1, name: "headset"},
{id: 5, name: "charger"}
]
}
];
例如,它们显示为
-> Phone name
-> accesories
现在是我的案例
如果我更新与手机有关的配件,我想更新显示的记录,但只更新与更新的配件有关的记录。我不想更新整个操作视图,只想更新与更新的附件
绑定的记录我该怎么做?
//编辑
我已经创建了一个手动解决方法
$scope.editAcc = function (idx) {
for(var i = 0; i < $scope.phones.length; i++) {
var tmpPhone = $scope.phones[i];
for(var j = 0; j < tmpPhone.accessories.length; j++) {
var acc = tmpPhone.accessories[j];
if(acc.id == idx) {
acc.name = acc.name + " Z";
}
}
}
// var acc = $ scope.accessories [idx]; // acc.name = acc.name +“X”; };
但IMO应该有一些功能来做到这一点
答案 0 :(得分:1)
我认为有一种内置的方法可以通过JSON路径更新值,但您至少可以使用angular.forEach()
来遍历集合:
$scope.editAcc = function (idx) {
angular.forEach($scope.phones, function (t) {
angular.forEach(t.accessories, function(acc){
(acc.id == idx) ? (acc.name += " Z") : null;
});
});
}
的 DEMO 强>