(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?
淡淡烟草味2017-05-15 17:07:33
Because a
和b
都是原始数据类型,在声明object
的时候,向arr
里填入的就是a
和b
corresponds to the string itself
So 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.
曾经蜡笔没有小新2017-05-15 17:07:33
You can use $watch
$scope.$watch('a',function(v){
$scope.object.arr
});
$scope.$watch('b',function(v){
$scope.object.arr[1] = v;
});
滿天的星座2017-05-15 17:07:33
$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.