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屋!