AngularJS access parent scope from child controller

前端 未结 9 1936
南方客
南方客 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:47

    Perhaps this is lame but you can also just point them both at some external object:

    var cities = [];
    
    function ParentCtrl() {
        var vm = this;
        vm.cities = cities;
        vm.cities[0] = 'Oakland';
    }
    
    function ChildCtrl($scope) {
        var vm = this;
        vm.cities = cities;
    }
    

    The benefit here is that edits in ChildCtrl now propogate back to the data in the parent.

提交回复
热议问题