Lately, I've been exploring the use of named controllers in my code and find it to be a much better alternative to using traditional $scope variables. It makes accessing parent controller attributes and functions within the DOM seamless. See an example below.
<div ng-controller="parentCtrl as parent">
<div ng-controller="childCtrl as child">
Parent age is {{ parent.age }}
Child age is {{ child.age }}
</div>
</div>
I'm now curious about how to utilize the same named controller within the JavaScript file for Angular. Something along the lines of what's shown below.
angular.controller('childCtrl',function(...) {
var vm = this;
this.age = 24;
console.log(parent.age);
});
I understand that the parent needs to be injected before being used. In my current project, I'm working with RequireJS alongside UI Router which eliminates the need for angular.controller
and similar constructs. Can anyone provide me with a solution using Plain vanilla Angular or with the help of RequireJS?