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 answershow 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
Post a Comment