AngularJS access parent scope from child controller

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

    From a child component you can access the properties and methods of the parent component with 'require'. Here is an example:

    Parent:

    .component('myParent', mymodule.MyParentComponent)
    ...
    controllerAs: 'vm',
    ...
    var vm = this;
    vm.parentProperty = 'hello from parent';
    

    Child:

    require: {
        myParentCtrl: '^myParent'
    },
    controllerAs: 'vm',
    ...
    var vm = this;
    vm.myParentCtrl.parentProperty = 'hello from child';
    

提交回复
热议问题