Rick
Rick

Reputation: 8846

Able to access `$scope` and `this` using controller as syntax. Why?

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.

Upvotes: 0

Views: 64

Answers (1)

Manas
Manas

Reputation: 26

Very good in details explanation at

http://codetunnel.io/angularjs-controller-as-or-scope/

Hope this helps !

Upvotes: 1

Related Questions