javascript - 在另一个模块中的指令内引用的模块中定义的 Controller

标签 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 controller1 here? ,
        controllerAs: controller1Alias
    }

    });

// pop.directives some more...

})();

我有一个 notIndex.html (模板或片段)和一个在 notMain.js 中定义的该 html 的相应 Controller :

(function() {

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

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

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

});

})();

所以我的问题是:

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

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

感谢您的帮助。

最佳答案

您可以添加 app1 作为 mainApp 模块的依赖项,它应该能够通过 Controller 的名称引用 Controller 。像这样的事情:

(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 闭包中不可用。

关于javascript - 在另一个模块中的指令内引用的模块中定义的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26158803/

相关文章:

html - 使用 Borders Bootstrap 3 的最佳方式

javascript - 替换屏幕尺寸上的 div id 或 class

javascript - 将文本转换为 html javascript

javascript - 如何在 Angular-J 中使用 ArcGIS map ?

javascript - Angular JS - 如何检测到 ng-repeat 已经完成?

javascript - D3 V3 到 D3 V4 树数组使用层次结构重新排序错误

javascript - 出生日期搜索验证 angularjs

javascript - 如何在 Javascript 中对某些特定值使用后备,而不是未定义?

javascript - 在 Javascript 中从字符串中提取一部分

javascript - 使用 Parse 查询用户对象中的列 - Javascript