AngularJS access parent scope from child controller

前端 未结 9 1886
南方客
南方客 2020-11-22 07:30

I\'ve set up my controllers using data-ng-controller=\"xyzController as vm\"

I have a scenario with parent / child nested controllers. I have no problem

9条回答
  •  执念已碎
    2020-11-22 07:50

    I believe I had a similar quandary recently

    function parentCtrl() {
       var pc = this; // pc stands for parent control
       pc.foobar = 'SomeVal';
    }
    
    function childCtrl($scope) {
    
       // now how do I get the parent control 'foobar' variable?
       // I used $scope.$parent
    
       var parentFoobarVariableValue = $scope.$parent.pc.foobar;
    
       // that did it
    }
    

    My setup was a little different, but the same thing should probably still work

提交回复
热议问题