I recently encountered a problem with modifying an array using angularjs.
HTML code is as follows
title1 title2
js code is as follows
$scope.title1 = "标题1" $scope.title2 = "标题2" $scope.arrTitle = [$scope.title1, $scope.title2]; //我试着修改 $scope.arrTitle[0] = "xx";
But $scope.title1 is not modified? $scope.arrTitle[0] should be $scope.title1 when printed? Could you please give me some advice on how to modify it? Thanks.
$scope.arrTitle is already a new variable (array)
When you modify $scope.arrTitle[0], you only modify the data of its first element.
$scope.arrTitle = [$scope.title1, $scope.title2]; Just assign an initial value to the array.
When you want to change $scope.arrTitle[0], $scope.title1 will also change, then use $scope.$watch
In fact, you can declare arrTitle as an object