gpt4 book ai didi

javascript - 为监视数组分配多个值但读取一次更改

转载 作者:行者123 更新时间:2023-11-29 21:53:38 25 4
gpt4 key购买 nike

<分区>

分配一个数组的方法是什么,它被监视,所以对数组的更改只会传播一次?

$scope.data = [field1:'1',field2:'2',field3:'3'];

scope.$watch('data', function (newVal) { /*...*/ }, true);

$scope.data['field1'] = '3';
$scope.data['field2'] = '2';
$scope.data['field3'] = '1';

这会触发 watch 3 次,是否可以将赋值作为值的集合?

我想也许可以处理“数据”的副本并在我修改副本后分配给它?

var copyofdata = $scope.data;
copyofdata['field1'] = '3';
copyofdata['field2'] = '2';
copyofdata['field3'] = '1';
$scope.data = copyofdata;

但也许有更优雅的解决方案不涉及临时复制?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com