2016-03-19 119 views
1

您好,請原諒我的英語。在Angular js的不同文件中使用多個模塊

我的問題是這樣的:我是一個初學者角度JS(目前1.4版本的學習),我想我的應用程序在兩個模塊像這樣分離:

app.js

(function(){ 
var app = angular.module('store', ['store-products', 'ngAnimate', 'ui.bootstrap']); 



    app.controller('StoreController', function(){ 
     ... 
     }); 

    app.controller('ReviewController', function(){ 
     ... 
     }; 

     this.rateOnClick = function(){ 
      ... 
     }; 

     this.addReview = function(product){ 
      ... 
     }; 
     }); 


    . 
    . 
    . 
})(); 

products.js

(function(){ 

    var app = angular.module('store-products', []); 

    app.directive('productTitle', function(){ 
      ... 
     }); 

    app.directive('productPanels', function(){ 
      ... 

     }); 
})(); 

的index.html

<html> 
    <head> 
    <title>My Angular App!</title> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-animate.js"></script> 
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.2.4.js"></script> 
    <link href="styles.css" rel="stylesheet"> 
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"> 
    <script src="app.js"></script> 
    <script src"products.js"></script> 
... 
</html> 

我所有的文件都在同一個文件夾中。 app.js是主模塊,所以據我瞭解包括都很好,但我仍然得到錯誤:

Uncaught Error: [$injector:modulerr] Failed to instantiate module store due to: 
Error: [$injector:modulerr] Failed to instantiate module store-products due to: 
Error: [$injector:nomod] Module 'store-products' 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. 

不能someon弄清楚爲什麼發生這種情況?

+0

你指的是應用程序的名稱? –

+0

這可能有所幫助:http://www.codeproject.com/Articles/862602/Define-multiple-Angular-apps-in-one-page –

回答

1

大聲笑,只是想通了,問題是行:

<script src"products.js"></script> 

固定這樣的:

<script src="products.js"></script> 
0

您需要在您的應用程序腳本之前包含您的產品腳本。

+0

我顛倒了順序,同樣的錯誤 –

+0

這應該解決了你的問題。你確定控制檯中沒有其他錯誤嗎?通常當你看到這個,你的腳本按照正確的順序被引用時,products.js中有一些語法錯誤,導致連鎖反應錯誤,導致你報告的模塊注入錯誤。 – Lex

+0

說真的,我甚至創建了一個沒有指令或控制器的新模塊,名爲test.js,並試圖包含在內,但同樣的錯誤。 –

0

不包括商店的產品在app.js的代碼,因爲在這個[]我們添加的指令,並且沒有指令名「商店產品的

+0

不知道是誰投了票,但這個答案不正確。 OP所做的是有效的,並且是其他Angular模塊作爲依賴注入到當前正在定義的模塊中的方式。 – Lex

相關問題