[英]Angularjs $watch tree array
我有一個像這樣的樹數組:
Array
(
[attributes] => Array
(
[attribute_group_id_6] => Array
(
[id] => 6
[name] => Processor
[items] => Array
(
[0] => Array
(
[product_id] => 40
[attr_id] => 1
[attr_text] => demo
[attr_group_id] => 6
[attr_group_order] => 1
[attr_group_name] => Processor
)
[1] => Array
(
[product_id] => 28
[attr_id] => 2
[attr_text] => 10
[attr_group_id] => 6
[attr_group_order] => 5
[attr_group_name] => Processor
)
[2] => Array
(
[product_id] => 28
[attr_id] => 3
[attr_text] => 777
[attr_group_id] => 6
[attr_group_order] => 3
[attr_group_name] => Processor
)
)
)
)
)
目標是購買items.[N].product_id
.product_id更改。
該代碼可以看到更改,但是可以一次看到所有3個更改。
$scope.$watch('filterData.attributes.items', function(val, old) {
console.log('watch');
}, true);
console:
watch
watch
watch
值通過文本字段更改:
問題是-如何僅觀看一項更改? (attributes.attribute_group_id_6.items.[N].product_id)
您的回調會觸發整個items
Array中的每次更改。 您真正需要的是每個項目的偵聽器,您可以在循環中創建該偵聽器-盡管JSHint可能不喜歡這樣。
for (var i = 0; i < $scope.filterData.attributes.items.length; i++) {
$scope.$watch('filterData.attributes.items[' + i + ']', function(val, old) {
console.log('watch');
}, true);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.