Pure Controller objects

Viewed 142

In AngularJS, a Controller itself is an object defined by a Javascript constructor function and this constructor function is a callback to .controller method.

var myapp=angular.module('scopeExample', []);
myapp.controller('MyController',function MyController($scope){
  $scope.name="john";
  this.num=700;
}); 

In the above example, MyController is the constructor function which creates the Controller object with one property (num). I have three queries upon that:

  • Actually, what is the use of the Controller object in that case?
  • Does it have some more properties not visible and is it accessible from outside Angular?
  • How it is interconnected to its scope which in turn is another object?

I came upon the following queries because of the controller as syntax which creates a controller object that is a property of controller's scope and therefore easily accessible, e.g.

<div ng-app="scopeExample" ng-controller="MyController as ctrl">
    <input id="box" ng-model="ctrl.num"> equals {{ ctrl.num }}   
</div>

<script> 
angular.module('scopeExample', [])
.controller('MyController', [function () {
     this.num=12;
}]);
</script>     

var x=angular.element('#box').scope().ctrl;    //x is the controller object itself
1 Answers
Related