One way data binding inside ng-click

≯℡__Kan透↙ 提交于 2019-12-13 03:14:53

问题


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 will change depends on checkbox click. I have done this part.

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> 

The problem is once the button is clicked, two way data binding is implemented. arrayB will change everytime arrayA changes.

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.


回答1:


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

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);

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.



来源:https://stackoverflow.com/questions/50850494/one-way-data-binding-inside-ng-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!