Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用“时,如何在子控制器中引用父控制器的范围?”;“作为控制人”;语法?_Javascript_Angularjs_Angularjs Scope - Fatal编程技术网

Javascript 使用“时,如何在子控制器中引用父控制器的范围?”;“作为控制人”;语法?

Javascript 使用“时,如何在子控制器中引用父控制器的范围?”;“作为控制人”;语法?,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,在myParentController中,我当前拥有: $scope.grid = { $index: null, view: [], data: [] }; .controller('ChildController', ['$Scope', function ($Scope) { $Scope.grid.$index =

在my
ParentController
中,我当前拥有:

   $scope.grid = {
                $index: null,
                view: [],
                data: []
            };
   .controller('ChildController', ['$Scope',
        function ($Scope) {

            $Scope.grid.$index = null;
            $Scope.grid.view = [];
            $Scope.grid.data = [];
在my
ChildController
中,我当前拥有:

   $scope.grid = {
                $index: null,
                view: [],
                data: []
            };
   .controller('ChildController', ['$Scope',
        function ($Scope) {

            $Scope.grid.$index = null;
            $Scope.grid.view = [];
            $Scope.grid.data = [];
据我所知,这允许我在
ChildController
并且还具有可在父控制器内的$Scope.grid.data上操作的函数

现在我想开始使用“as controller”语法。因此,如果我正确理解了这一点,我需要在两个HTML页面中对HTML中的控制器进行如下编码:

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

<div data-ng-controller="ChildController as child" ..
   this.grid = {
                $index: null,
                view: [],
                data: []
            };
如果我这样做,那么在我的
ChildController
中,我应该如何引用grid.view?我是否以某种方式将“
parent
”传递到
ChildController
,然后在我的孩子身上使用:

parent.grid.view = [];

最后我听说“as controller”语法是实验性的。现在它已经过了那个阶段了吗?

只要不更改代码,“As”语法就没有任何区别

如果您将家长控制器更改为

this.grid = {
  $index: null,
  view: [],
  data: []
};
然后,您仍然需要在
ChildController
中使用
$scope
引用,但您添加了另一个属性:

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

        $Scope.parent.grid.$index = null;
        $Scope.parent.grid.view = [];
        $Scope.parent.grid.data = [];
正如您所见,它不仅更加复杂,而且还将代码与HTML耦合:如果您决定将HTML更改为
ParentController As someController
您的
ChildController
中断

因此,即使使用“as”语法,您也希望
网格
成为
$scope
的属性