I'm building an Angular controller using controller as
syntax:
<body ng-controller="ctrl as myCtrl">
<p>accessed via scope resolution: {{ foo }} </p>
<p>accessed via controller resolution: {{ myCtrl.foo }}</p>
</body>
In the controller I have this:
myApp.controller('ctrl', function($scope) {
this.foo = 'Controller\'s foo';
$scope.foo = '$scope\'s foo';
});
The code above works and prints bot controller's foo
and $scope's foo
.
- Why?
- Is this just a case of "unspecified behavior"?
- Is there ever a time when you would want to use both in the real world?