简体   繁体   English

使用“作为控制器”语法时,如何在子控制器中引用父级的作用域?

[英]How do I refer to the scope of a parent inside a child controller when using the “as controller” syntax?

In my ParentController I currently have: 在我的ParentController我目前有:

   $scope.grid = {
                $index: null,
                view: [],
                data: []
            };

In my ChildController I currently have: 在我的ChildController我目前有:

   .controller('ChildController', ['$Scope',
        function ($Scope) {

            $Scope.grid.$index = null;
            $Scope.grid.view = [];
            $Scope.grid.data = [];

From what I understand this allows me to populate $Scope.grid.data in the ChildController and also have functions that can operate on the $Scope.grid.data inside the Parent Controller . 据我了解,这使我可以在ChildController填充$ Scope.grid.data,并且还具有可以在Parent Controller的$ Scope.grid.data上进行操作的函数。

Now I would like to start using the "as controller" syntax. 现在,我想开始使用“作为控制器”语法。 So if I understand this correctly I need to code inside the two HTML pages the controllers in my HTML like this: 因此,如果我正确理解了这一点,则需要在两个HTML页面中编写代码,例如HTML中的控制器:

<div data-ng-controller="ParentController as parent" ..

<div data-ng-controller="ChildController as child" ..

And my parent controller like this: 而我的父控制器是这样的:

   this.grid = {
                $index: null,
                view: [],
                data: []
            };

If I do this then in my ChildController how should I refer to the grid.view? 如果执行此操作,那么在ChildController应如何引用grid.view? Do I somehow pass ' parent ' into the ChildController and then in my child use: 我是否以某种方式将“ parent ”传递给ChildController ,然后在我的孩子中使用:

parent.grid.view = [];

Finally I heard that the "as controller" syntax was experimental. 最后,我听说“作为控制器”语法是实验性的。 Has it now got past that stage ? 现在已经过去了吗?

As long as you don't change your code the "as" syntax doesn't make any difference. 只要您不更改代码,“ as”语法就不会有任何区别。

If you changed ParentController to 如果您将ParentController更改为

this.grid = {
  $index: null,
  view: [],
  data: []
};

then you would still need a $scope reference in your ChildController , but you add another property: 那么您仍然需要在ChildController使用$scope引用,但要添加另一个属性:

   .controller('ChildController', ['$Scope',
    function ($Scope) {

        $Scope.parent.grid.$index = null;
        $Scope.parent.grid.view = [];
        $Scope.parent.grid.data = [];

As you can see it's not only more complicated, but you also couple the code to the HTML: If you decide to change your HTML to ParentController as someController your ChildController breaks. 正如你可以看到它的不仅更加复杂,但你也把代码耦合到HTML:如果你决定改变你的HTML ParentController as someControllerChildController休息。

So even with the "as" syntax you want grid to be a property of $scope . 因此,即使使用“ as”语法,您也希望grid成为$scope的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用Typescript时,如何从子控制器访问父控制器中的作用域函数? - How can I access a scope function in a parent controller from a child controller when using Typescript? 使用控制器作为语法时,如何从父指令继承属性? - How do I inherit properties from parent directive when using the controller as syntax? 如何从父控制器引用子作用域? - How to reference the child scope from the parent controller? 如何在Angular中访问父控制器中的子控制器范围? - How to access Child Controller Scope in Parent Controller in Angular? 如何在子控制器中访问父控制器$ scope值 - How to access parent controller $scope values in child controller 当其他子控制器更改了该范围属性时,如何在另一个控制器中为父范围的作用域属性调用监视侦听器 - How to call watch listener in another controller for scope property of parent scope when that scope property is changed by other child controller 如何从父指令/控制器访问指令范围? - How do I access a directive scope from a parent directive/controller? 如何在AngularJS中的父控制器或作用域中访问FormController - How do I access FormController in parent controller or scope in AngularJS 如何在子作用域中修改父作用域中的控制器属性? - How to modify controller property in parent scope from a child scope? 使用“ controller as”语法时,如何访问控制器中的“ this”? - How can I access the “this” in my controller when using “the controller as” syntax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM