這裏的Plunker: https://plnkr.co/edit/itgraL2X6gCHCs7tgSZi角修改屬性時的forEach或純粹爲了
我有2個集合
$scope.params = [{
id: 1,
nombre: 'bloom'
}, {
id: 2,
nombre: 'ctotal'
}, {
id: 3,
nombre: 'coliformes'
}];
$scope.puntos = [{
id: 1,
nombre: 'votator'
}, {
id: 2,
nombre: 'vw1'
}, {
id: 3,
nombre: 'preparador'
}];
我在控制器代碼看起來像:
angular.forEach($scope.puntos, function(punto, key) {
punto.params = $scope.params;
angular.forEach(punto.params, function(pa, key) {
pa.otherProp = "xxxxx";
});
});
由於你可以看到,我想將params集合分配給puntos集合中的每個項目。然後,我分配到集合中的otherProp屬性的值裏面的物品.....我永遠不會改變的主要$ scope.params ....但如果我同時輸出這也發生了變化
然後我輸出:
Puntos
{"id":1,"nombre":"votator","params":[{"id":1,"nombre":"bloom","otherProp":"xxxxx"},{"id":2,"nombre":"ctotal","otherProp":"xxxxx"},{"id":3,"nombre":"coliformes","otherProp":"xxxxx"}]}
{"id":2,"nombre":"vw1","params":[{"id":1,"nombre":"bloom","otherProp":"xxxxx"},{"id":2,"nombre":"ctotal","otherProp":"xxxxx"},{"id":3,"nombre":"coliformes","otherProp":"xxxxx"}]}
{"id":3,"nombre":"preparador","params":[{"id":1,"nombre":"bloom","otherProp":"xxxxx"},{"id":2,"nombre":"ctotal","otherProp":"xxxxx"},{"id":3,"nombre":"coliformes","otherProp":"xxxxx"}]}
Params
{"id":1,"nombre":"bloom","otherProp":"xxxxx"}
{"id":2,"nombre":"ctotal","otherProp":"xxxxx"}
{"id":3,"nombre":"coliformes","otherProp":"xxxxx"}
起初我因子評分,這個問題是通過使用angular.forEach引起....所以我決定用一個純粹的.....這個問題似乎不是
for (var i = 0; i < $scope.puntos.length; i++) {
var punto = $scope.puntos[i];
punto.params = [];
punto.params = $scope.params;
for (var j = 0; j < punto.params.length; j++){
var pa = punto.params[j];
pa.otherProp = 'yyyyyy';
}
}
當然,這是一個simmple例子......我真正想要的是更復雜,但是,這是阻止我...
爲什麼PARAMS收集修改,即使我沒有手動改變呢? 霍伊預防呢?
在此先感謝
你每次 – charlietfl