javascript - Specifying controller in directive definition does not create new scope -


I ran on a unique intuitive aspect of AngularJS instructions and I'm curious if there is a logical reason for this behavior When I specify a controller in the definition of the director:

  Angular Modules ('MyApp.directives', []). Directional ('myDirective, function') {templateUrl: "my-template Html", Controller: "Mac Controller"}  

The scope of the template of the instructions is the same for all instances of the instructions

However, if I specify an Administrator in the definition of

  Angular. Module ('MyApp.directives', []). Directive (' myDirective Define the controller directly on the director using function () {templateUrl}: "my-template.html"};  

and instead using ng-controller Do:

 <  

Each context of gets the scope of your own child. / P>

I hope that the same results will be achieved in these ways, but apparently the angular controller treats the scope differently, depending on how the oller has been declared, there is a reason for this difference ?

The reason for this, I believe, is the fact that one As instructed Ribhasha a singleton so when you enter the controller as a property to return an object of singleton, it is common to all instances.

But when you define markup with the NG-controller, then the NG-controller directive is every element, so it creates a new controller (and thus a new area) for each director example .


Comments