angularjsangularjs-scopeangularjs-bootstrap

Controller initialization in AngularJS


<html>
      <body>
           <div ng-controller="TestController">
                 Framework is {{name}}
           </div>
      </body>
</html>

Am making the above piece of html code into angular manually in the script.

var app = angular.module('TestModule',[]);
angular.bootstrap(document, app);

I want to know the difference, advantages, disadvantages in initializing the controller in the below two ways.

Model 1:

var app = angular.module('TestModule',[]);
app.controller('TestController',function($scope){
     $scope.name="Angualar";
});
angular.bootstrap(document, app);

Model2:

var app = angular.module('TestModule',[]);
window['TestController'] = function($scope){
      $scope.name="Angualar";
};
angular.bootstrap(document, app);   

Both the above models works same. When does the controller actually creates. when the scope gets creates for the controller. Kindly share your comments.


Solution

  • Model2 is the bad way to do it. Use Model1, its better and angular's way to create controllers.

    In Model2 you are polluting global window scope.