Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.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 - Fatal编程技术网

Javascript 将变量从父指令传递到子指令

Javascript 将变量从父指令传递到子指令,javascript,angularjs,Javascript,Angularjs,我有两条指示。父母和孩子。我想将一个变量传递给parent指令,并使多个子指令可以访问它。以下是指令: directives.directive('supNavDirective', function () { return { restrict: "E", //declare by element replace: true, scope: { navtree: '=' },

我有两条指示。父母和孩子。我想将一个变量传递给parent指令,并使多个子指令可以访问它。以下是指令:

directives.directive('supNavDirective', function () {
    return {
        restrict: "E",    //declare by element
        replace: true,
        scope: {
            navtree: '='
        },
        controller: function ($scope, $element) {
            $scope.returnNavTree=function(){
                return $scope.navtree
            }
        },
        link: function (scope, element, attrs) {
        }
    }
})


directives.directive('collection', function () {
    return {
        require:"supNavDirective",
        restrict: "E",    //declare by element
        link: function (scope, element, attrs,superDir) {
         scope.collection = superDir.returnNavTree()
        },
        template: "<ul class=\"nav nav-list tree\"><member ng-repeat=\"member in collection\" member=\"member\"></member></ul>"
    }
})
以下是html:

<superNavDirective collection navtree="analyticsNavTree"></superNavDirective>

child指令似乎无法获取变量。为什么不呢?

首先,需要父控制器的正确语法是:

require:"^supNavDirective"
其次,必须将returnNavTree方法添加到控制器实例本身,而不是$scope,才能从child指令访问它,如下所示:

...
controller: function ($scope, $element) {
  this.returnNavTree = function(){
    return $scope.navtree
  }
},
...

首先,要求父控制器的正确语法是:

require:"^supNavDirective"
其次,必须将returnNavTree方法添加到控制器实例本身,而不是$scope,才能从child指令访问它,如下所示:

...
controller: function ($scope, $element) {
  this.returnNavTree = function(){
    return $scope.navtree
  }
},
...

谢谢你的回答。你的回答与文件相符。我是个新手,感觉有点失落。我不明白为什么我的示例中的child指令无法呈现我的示例中的父消息。这里有一个plunker展示了我想要实现的目标:嗨,很高兴它能帮上忙。对于新的问题,我建议您添加一个新的问题,而不是适当的标题和描述。谢谢您的回答。你的回答与文件相符。我是个新手,感觉有点失落。我不明白为什么我的示例中的child指令无法呈现我的示例中的父消息。这里有一个plunker展示了我想要实现的目标:嗨,很高兴它能帮上忙。对于新的问题,我建议您添加一个新的问题,而不是适当的标题和描述。