2014-02-20 89 views
7

我是AngularJS的新手,正在創建一個使用Grunt構建的應用程序。如何解決內置AngularJS應用程序的依賴問題?

當我構建和運行我的應用程序,我注意到有關依賴的加載順序的一些問題:

Uncaught Error: [$injector:nomod] Module 'mycompany.admin.forms' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. 
http://errors.angularjs.org/1.2.13/$injector/nomod?p0=mycompany.admin.forms 

在內置的應用程序文件,宣告前它正在使用的模塊:

angular.module('mycompany.admin.forms').controller('editController', ['$scope', function($scope) { 
    // ... 
}]); 

angular.module('mycompany.admin.forms', []) 

    .config(['$routeProvider', function($routeProvider) { 
    // ... 
    }]); 

下面是我的項目的gruntfile.js有關片段:

grunt.initConfig({ 
    distdir: 'build', 
    pkg: grunt.file.readJSON('package.json'), 
    src: { 
     js: ['src/**/*.js'], 
    }, 
    concat: { 
     dist: { 
      src: ['<%= src.js %>', '<%= src.jsTpl %>'], 
      dest: '<%= distdir %>/admin/app.js' 
     } 
    } 
    ... 
}); 

我知道我可以解決通過將給定模塊的所有源代碼放入一個文件中;但是,我想嘗試將每個事物(每個控制器,每個服務等)保存在自己的文件中。

如何解決此依賴性加載順序問題?

回答

5

什麼命令是grunt連接你的文件?看起來你只是在讀一個目錄,並且如果模塊聲明所在的javascript文件位於控制器所在的文件之後,則連接文件將以錯誤的順序構建。

可能最簡單的解決方案是顯式地列出包含您的模塊聲明的文件(使用[])作爲源數組中的第一個文件。

+0

這就是我的想法。我剛剛嘗試過,而且很有效。 –

+0

我現在正在做的不是列出我的Gruntfile中的單個文件,而是使用一個模式(我的模塊定義在* .module.js文件中),所以他們的名字首先被渲染爲''{.tmp,<%= config .client%>}/{app,components}/**/*。module.js','{.tmp,<%= config.client%>}/{app,components}/**/*。js' '。這可能不適用於更復雜的模塊嵌套結構。 – yorch