angularjs - Accessing parent scope from a child controller with ControllerAs syntax -



angularjs - Accessing parent scope from a child controller with ControllerAs syntax -

this question has reply here:

accessing inherited scope controller approach 2 answers

how can accomplish next using controller-as approach:

app.controller("parentctrl", function($scope){ $scope.parentobj = {prop1: "not set", prop2: "something"}; $scope.dosomething = function(){...} }) .controller("childctrl", function($scope){ $scope.parentobj.prop1 = "changed"; }); <div ng-controller="parentctrl"> {{prop1}} <div ng-controller="childctrl"> {{prop2}} <button ng-click="dosomething()">do</button> </div> </div>

without making assumptions how parent controller aliased in view, i.e. no {{pc.prop2}}.

in other words, benefit scope inheritance while using controller-as approach. question how?

app.controller("parentctrl", function(){ this.parentobj = {prop1: "not set", prop2: "something"}; this.dosomething = function(){...} }) .controller("childctrl", function($scope){ // $scope.parentobj undefined! });

when using as syntax define controller access parent scope variable in child controller utilize next :

var userdata = $scope.parentctrl.user;

where parentctrl name of parent controller using as syntax , user variable defined in same controller.

angularjs angularjs-scope angularjs-controller

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -