我有一个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
});
})();
所以我的问题是:
如何在notMain.js的directive1中引用controller1?
我不能在 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/