0

I just started to wrap my head around controllers, however in a tutorial for learning ui-router I saw my presenter using his controller with "as" syntax. I researched more about it and saw it instantiates a controller as a class.

When should "as" syntax be used?

SD Dev
  • 335
  • 3
  • 10
  • This looks like a good resource: http://stackoverflow.com/questions/21287794/angularjs-controller-as-syntax-clarification – plong0 Jul 30 '16 at 16:25

2 Answers2

0

situation 1:- Many times we use directives in our view. Directives may have used scope object inside it. now we have two scope object one of controller's and other of directive's.

situation 2: When we two controllers in same html file we need to have mapping of variable then we need that syntax(not recomanded)

0

using either:

controller: 'MyController as MyCtrl'

or

controller: 'MyController',
controllerAs: 'MyCtrl'

simply exposes the controller object on scope. This lets you access its properties/methods directly from your template (ex. {{MyCtrl.foo}} or ng-click="MyCtrl.doStuff()") or inheriting scopes.

plong0
  • 2,140
  • 1
  • 19
  • 18