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

Javascript 在另一个模块的指令内引用的模块中定义的控制器

Javascript 在另一个模块的指令内引用的模块中定义的控制器,javascript,html,angularjs,Javascript,Html,Angularjs,我有一个index.html(主入口点)和一个main.js文件,比如: (function() { var pop = angular.module('mainApp', []); pop.directive = ('directive1', function() { return { restrict: 'E', templateUrl: notIndex.html, controller: // how to reference

我有一个index.html(主入口点)和一个main.js文件,比如:

(function() {

var pop = angular.module('mainApp', []);

pop.directive = ('directive1', function() {
    return {
        restrict: 'E',
        templateUrl: notIndex.html,
        controller: // how to reference controller1 here? ,
        controllerAs: controller1Alias
    }

    });

// pop.directives some more...

})();
我在notMain.js中定义了一个notIndex.html(模板或片段)和一个相应的html控制器,比如:

(function() {

var app = angular.module('app1', []);

app.controller = ('controller1', function() {

    function1...
    function2...
    .
    .
    .
    functionN

});

})();
因此,我的问题是:

  • 如何在notMain.js的directive1中引用controller1

  • 我不能在notIndex.html中有标签(它不像模板那样有标题或正文),对吗?如何在此html中引用notMain.js中定义的AngularJS指令?我想是在index.html中吧


  • 谢谢您的帮助。

    您可以添加
    app1
    作为
    mainApp
    模块的依赖项,并且它应该能够通过控制器的名称引用控制器。大概是这样的:

    (function() {
        var pop = angular.module('mainApp', ['app1']);
    
        pop.directive = ('directive1', function() {
           return {
               restrict: 'E',
               templateUrl: 'notIndex.html',
               controller: 'controller1'
               controllerAs: 'controller1Alias'
           };
        });
    })();
    

    注意到我将
    app.directive
    更改为
    pop.directive
    ?这是因为
    app1
    在您的
    mainApp
    closure中不可用。

    谢谢您的回答。对于问题2,引用notMain.js的标记在index.html中声明对吗?之后notIndex.html能否调用notMain.js控制器?如果在notIndex.html中包含脚本标记,那么它将不起作用,您应该在index.html中包含脚本标记