Angularjs object assignment problem

Source code:

for(var i = 0; i < $scope.list.length; i++){
	if($scope.list[i].md_id == $scope.currentmetadatainfo.md_id){
		$scope.list[i] = $scope.currentmetadatainfo;
	}
}

Code Explanation:

    There is a list in my JS code for repeat display, where currentmetadatainfo is the current editing object. After the currentmetadatainfo object is changed, I want to refresh the data corresponding to the list list . The result is that the list is refreshed, but the value of the currentmetadatainfo object continues to be changed. Due to the two-way binding mechanism of AngularJS, the value of list[i] will also change, which is displayed. The data in the list also changed accordingly.

Solution: When assigning a variable to a variable in JS, a copy is usually made, but if the value is an object (Object), the address of the object will be passed in.

Solution:

AngularJS has encapsulated the relevant workarounds:

for(var i = 0; i < $scope.list.length; i++){
	if($scope.list[i].md_id == $scope.currentmetadatainfo.md_id){
		var currentmetadatainfo =  angular.copy($scope.currentmetadatainfo);
		$scope.list[i] = currentmetadatainfo;
	}
}
Original: https://my.oschina.net/yiqu/blog/547125

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324819550&siteId=291194637