(1) Two variables are defined in the controller
$scope.a="aaaaa";
$scope.b="bbbbb";
(2) Also define an object to save
$scope.object={
arr:[$scope.a,$scope.b]
}
(3) Now dynamically modify the values of $scope.a
and $scope.b
.
But the value in $scope.object.arr
has not changed. Why is this? Shouldn't it be updated in real time?
Because
a
和b
都是原始数据类型,在声明object
的时候,向arr
里填入的就是a
和b
corresponds to the string itselfSo when you modify it later
$scope.a
和$scope.b
时,$scope.object
it doesn’t change along with it.This is like, I have two apples, gave you one, and then I took a bite of mine, because the apples look exactly the same (assuming, it is a false proposition), so I expect that the apple in your hand will also appear A bitten gap.
You can use $watch
The default is shallow traversal
$scope.a, $scope.b are strings, and assignment is equivalent to directly assigning strings to arrays.
If you want to achieve your goal, you can monitor a and b. When a and b change, assign a value to the object.