Royi Namir
Royi Namir

Reputation: 148524

AngularJs's "Controller as" syntax - Controller representation ?

I was wondering :

Just as without using controller as syntax , I'd have to do :

<body ng-controller="ParentCtrl">
  <input ng-model="name" /> {{name}}

  <div ng-controller="ChildCtrl">
    <input ng-model="name" /> {{name}} - {{$parent.name}}
  </div>
</body>

And now with the Controller as syntax , I can do :

<body ng-controller="ParentCtrl as parent">
  <input ng-model="parent.name" /> {{parent.name}}

  <div ng-controller="ChildCtrl as child">
    <input ng-model="child.name" /> {{child.name}} - {{parent.name}}
  </div>
</body>

Which is great , but what about the controller itself ?

With the first example , I could do :

....controller('Controller', function($scope) {
   // do something with $parent.$scope...

But now , after using this :

....controller('Controller', function() { //this.mySomething....

Question :

How would I reference the parent ? ( in the alias way!)

I mean , NG come here to help us by using aliases to scope via parent && child ,

But is there any representation to that "help" in the controller ?

Upvotes: 0

Views: 636

Answers (1)

LetterEh
LetterEh

Reputation: 26696

No, there isn't.

The goal of the Controller as ... was to make dealing with models more natural, and to remove all of the mess of dealing with $scope, except when needed.

Models don't really have $parents.

$scope has a parent-$scope.

But if I have a parent Controller as "Bike" and a nested controller "Doorknob"...

Doorknob might have .turn() and .type and .locked, but it doesn't have a "Bike", any more than all "Bike"s have "Doorknobs".

You still have direct-access to the $scope in the controller, so you can add your own inheritance, and build your own links -- you can also still reference other $scope properties in your view...

Upvotes: 1

Related Questions