javascript - Array modification problem in angularjs
PHP中文网
PHP中文网 2017-07-05 11:04:58
0
2
979

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.

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
扔个三星炸死你

$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

<span ng-bind="arrTitle.title1"></span>
<span ng-bind="arrTitle.title2"></span>
$scope.arrTitle = {
    title1: "标题1",
    title2: "标题2"
};
//修改 
$scope.arrTitle.title1 = "xx";
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template