I'm following a book, and using this version of Angular: https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js[1]
This is my template:
<div ng-controller="ParentController">
<div ng-controller="ChildController">
<a ng-click="sayHello()">Say hello</a>
</div>
{{ person }}
</div>
This is my controller code:
app.controller('ParentController', function ($scope) {
$scope.person = { greeted: false };
});
app.controller('ChildController', function ($scope) {
$scope.sayHello = function () {
$scope.person = { name: "Ari Lerner", greeted: true };
}
});
I noticed my code doesn't doesn't update the template as expected unless I change my sayHello method to this:
$scope.sayHello = function () {
$scope.person.name = "Ari Lerner";
$scope.person.greeted = true;
};
Any insight as to why this might be? I was under the assumption that updating person would be reflected in the DOM.
Using 1.4.2 yields the same result.
Thinking that maybe the properties are somehow indexed differently, I tried the following:
$scope.person.name = { greeted: true, name: "Ari Lerner" };
(switched greeted and name)
Wild speculation: It seems to me that something in Angular is holding on to the original object that was assigned to $scope.person and setting $scope.person to a new object "loses" the data binding. Is this true?