2014-10-02 95 views
1

我有一個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定義這個網站相應的控制器說:

(function() { 

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

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

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

}); 

})(); 

所以我的問題是:

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

  2. 我不能在notIndex.html中有標籤(它沒有模板的標題或正文),對不對?我如何從這個html引用在notMain.js中定義的AngularJS指令?在index.html中,我想?

感謝您的幫助。

回答

1

您可以添加app1作爲mainApp模塊的依賴關係,它應該能夠通過控制器的名稱引用控制器。事情是這樣的:

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

    pop.directive = ('directive1', function() { 
     return { 
      restrict: 'E', 
      templateUrl: 'notIndex.html', 
      controller: 'controller1' 
      controllerAs: 'controller1Alias' 
     }; 
    }); 
})(); 

注意到,我改變app.directivepop.directive?這是因爲app1在您的mainApp關閉中不可用。

+0

感謝您的回答。對於問題2,我是對的,

相關問題