简体   繁体   English

Angular指令-找不到控制器指令

[英]Angular directive - controller not found directive

Controller 'appLeft', required by directive 'appContent', can't be found! 找不到指令“ appContent”所需的控制器“ appLeft”!

//app-nav(left)
    app.directive('appLeft', function ()
    {
        return {
            restrict: 'E',
            replace: false,
            scope: {
                leftItem: "=leftItem"
            },
            controller:function($scope){
                this.title = $scope.leftItem;
            },
            templateUrl: 'res/tpl/app-left.html',
            link: function (scope, ele, attr)
            {
                scope.toggle=function(index){
                    scope.leftItem[index].isShow = !scope.leftItem[index].isShow;
                }
            }
        }
    });
    //app-content
    app.directive('appContent',function(){
        return {
            require:'^appLeft',
            restrict: 'E',
            replace:false,
            transclude:true,
            scope:{},
            templateUrl:'res/tpl/app-content.html',
            link:function(scope,ele,attr,appLeftCtrl){
                console.log(appLeftCtrl.title)
            }
        }
    });

Controller 'appLeft', required by directive 'appContent', can't be found! 找不到指令“ appContent”所需的控制器“ appLeft”!

make sure your required directive is outside of the child directive in the html DOM since the appLef directive is the parent directive. 由于appLef指令是父指令,因此请确保您所需的指令在html DOM的子指令之外。

<app-left>
     <app-content></app-content>
</app-left> 

According to Doc 根据Doc

When a directive uses require, $compile will throw an error unless the specified controller is found. 当指令使用require时,除非找到指定的控制器,否则$ compile将引发错误。 The ^ prefix means that this directive searches for the controller on its parents (without the ^ prefix, the directive would look for the controller on just its own element). 前缀^表示此伪指令在其父级上搜索控制器(没有^前缀,伪指令将仅在其自己的元素上查找控制器)。

Demo 演示

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM