While attempting to answer a question regarding sharing data between two separate controllers I ran into a question .
I usually use services for for this task and
When Angular sees
Does Not Work: {{badPerson.name}}
it sets up a $watch on object badPerson
. Looking at your controller, $scope.badPerson
is a reference to object DataService.badPerson
. All is fine so far... the problem happens here:
setActivePersonDoesNotWork: function (person) {
People.badPerson = person;
}
When this function executes, badPerson
is assigned a new/different object reference, but the controller is still $watching the old/original object reference.
The fix is to use angular.copy() to update the existing badPerson
object, rather than assigning a new reference:
setActivePersonDoesNotWork: function (person) {
angular.copy(person, People.badPerson);
}
This also explains why setActivePersonWorks()
works -- it does not assign a new object reference.