如何在使用requirejs

时间:2015-04-29 09:52:07

标签: angularjs requirejs angular-amd

描述我的障碍,一个小故事。我正在注入所有requirejs路径文件(基本上是指令),其中包含app.js中我的主模块的任何指令,这是完美的。它有点像

define(['angularAMD', 'angular-route', 'angular-resource', 'angularTranslate', 'angularTranslateLoaderStaticFiles', 'bootstrap', 'angular-idle', 'loadingMaskDirective', 'multiselectDirective', 'treeview.directive', 'tabs', 'checklist.directive'], function(angularAMD) { 
var app = angular.module("myApp", ["ngRoute", "ngResource", "ngTable", "myApp.directives", 'ui.bootstrap', 'flow']);
app.config(....){
/**some route provider**/
}...
angularAMD.bootstrap(app);
return app;

我已经定义了所有指令并将它们注入主模块。但是我想在他们的indivisual controller 中定义一些指令来减少初始负载。必须有一些方法。对!!

我的指令js文件中的代码看起来像..

define(['angular'], function() {
angular.module('myApp').directive('SomeDirective',
        ['$parse', '$compile', '$rootScope', '$filter', function($parse, $compile, $rootScope, $filter) {
.........

因此,当我尝试在app.js中的页面控制器中定义相同内容时,它不起作用。但是我很惊讶在这种情况下工厂功能有效但不是指令

任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:2)

您是否尝试使用app创建指令?

define(['app'], function(app) {
app.directive('SomeDirective',
        ['$parse', '$compile', '$rootScope', '$filter', function($parse, $compile, $rootScope, $filter) {
.........

angularAMD提供的替代方案是:

define(['angularAMD'], function(angularAMD) {
angularAMD.directive('SomeDirective',
        ['$parse', '$compile', '$rootScope', '$filter', function($parse, $compile, $rootScope, $filter) {
.........

第二种方法的好处是允许在加载app.js之前加载指令。有关详细信息,请参阅Loading Application Wide Module