I'm new to Angular but really enjoying its approach. I have an HTML file where I am initializing a variable with ng-init
in a <div>
element, where I'm also declaring a controller with the ng-controller
directive:
<div ng-controller="myCtrl" ng-init='foo="bar"'>
If I console.log
the $scope
object from the controller script I can see the foo
property listed among the others, but when I try to access it from the same script it gives me undefined
. I'm also using Batarang and it shows me a model for the <div>
-scope that also includes the foo
property.
I know from the second answer to Pass variables to AngularJS controller, best practice? that I can solve the problem by moving my ng-init
directive into an outer <div>
, but I would like to know what is really going on here behind the scenes. Any help greatly appreciated, thanks in advance.
EDIT
The order of the directives in the div
element does not matter. The problem is still there even if ng-init
is specified before ng-controller