ng-click 中的一种数据绑定方式 [英] One way data binding inside ng-click
问题描述
我的控制器中有两个 $scope 数组.
I have two $scope array inside my controller.
$scope.arrayA= [false, false, false, false, false, false];
$scope.arrayB= [false, false, false, false, false, false];
arrayA 会随着复选框的点击而改变.这部分我已经完成了.
arrayA will change depends on checkbox click. I have done this part.
arrayB 只会在点击按钮时将值更改为等于 arrayA.
arrayB will change values to be equal to arrayA only when a button is clicked.
<button type="button" ng-click="arrayB = arrayA" class="btn btn-search">Get Data</button>
问题是一旦点击按钮,就实现了双向数据绑定.每次 arrayA 更改时,arrayB 都会更改.
The problem is once the button is clicked, two way data binding is implemented. arrayB will change everytime arrayA changes.
我只想在单击按钮时更改 arrayB.有没有办法在 ng-click 中使用角度单向数据绑定 @
?你知道我们如何在 python 中将变量值传递为 varB = varA
.
I only want arrayB to change when the button is clicked. Is there a way to use the angular one-way data binding @
inside ng-click? You know like how we pass variables values in python as varB = varA
.
推荐答案
不是将 arrayA
直接赋值给 arrayB
,而是需要创建它的副本,以便两个变量不指向同一个对象.
Instead of assigning arrayA
directly to arrayB
, you need to create a copy of it, so that both variables don't refer to the same object.
arrayA = arrayB
上面的赋值只是让 arrayA
和 arrayB
指向同一个对象.点击按钮,你可以试试这个:
The above assignment simply makes arrayA
and arrayB
to refer to the same object. On click of the button, you may try this:
arrayB = arrayA.map(item => item);
这将确保创建数组的新副本,并且由于 varA
包含的值是原始值(布尔值),因此不会发生冲突.
This will ensure that a new copy of the array is created, and since the values varA
contains are primitive (boolean), there will be no conflicts.
这篇关于ng-click 中的一种数据绑定方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!