I'm working in Angular with parent/child Controller inheritance and am encountering some behavior that I do not understand.
Basically, I set a property on an object in a parent Controller, and set it to a different value in the child Controller. I output the value that was set in the parent Controller before the code in the child Controller should run. However, I am seeing that the child Controller code has already run and set the value.
Parent.Controller.js
testapp.controller('ParentController', function($scope) {
$scope.init = function() {
$scope.myValue = 'Primitive value, defined in ParentController';
$scope.myObject = {
value: "Object property value, defined in ParentController"
}
}
$scope.init();
});
Child.Controller.js
testapp.controller('ChildController', function($scope) {
$scope.init = function() {
$scope.myValue = 'Primitive value, set in ChildController';
$scope.myObject.value = "Object property value, set in ChildController";
}
$scope.init();
});
HTML Template
<div ng-controller="ParentController" class="outer">
1. Primitive value on scope: <span style="font-weight:bold">{{myValue}}</span>
<br/>2. Object value on scope: <span style="font-weight:bold">{{myObject.value}}</span>
<p>
<div ng-controller="ChildController" class="inner">
3. Primitive value on scope: <span style="font-weight:bold">{{myValue}}</span>
<br/>4. Object value on scope: <span style="font-weight:bold">{{myObject.value}}</span>
</div>
<p>
5. Primitive value on scope: <span style="font-weight:bold">{{myValue}}</span>
<br/>6. Object value on scope: <span style="font-weight:bold">{{myObject.value}}</span>
</div>
I would have expected #2 to show the value of the object that was defined by the Parent Controller, since the Child Controller should not yet have executed (the ng-controller directive for the ChildController is a couple lines south of this).
Here is a Plunk
If anyone could help me understand this behavior, I would really appreciate it.
Thanks,
Philip