I recently encountered a problem with modifying an array using angularjs.
HTML code is as follows
<span>title1</span>
<span>title2</span>
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.
扔个三星炸死你2017-07-05 11:06:58
$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
大家讲道理2017-07-05 11:06:58
In fact, you can declare arrTitle as an object
<span ng-bind="arrTitle.title1"></span>
<span ng-bind="arrTitle.title2"></span>
$scope.arrTitle = {
title1: "标题1",
title2: "标题2"
};
//修改
$scope.arrTitle.title1 = "xx";