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

maven fortify plugin : Unable to load build session with ID XXXXX .. See log file for more details -

c# - Primavera WebServices does not return any data -

android - Display emoji panel with genymotion - keyboard/touch input? -