2015-05-03 17 views
2

剛開始使用angularJS,我不確定爲什麼我不能使用模塊。不確定爲什麼'userApp'模塊不可用

第一個代碼片段是HTML(main.js)中引用的代碼片段。

'use strict'; 
 

 
angular.module('userApp') 
 
    .controller('MainCtrl', function($scope, $http) { 
 
    $scope.user = []; 
 
    $http.get('http://127.0.0.1:8080/collector/50001366562').success(function(data) { 
 
     $scope.user = data; 
 
    }) 
 
    })
<html ng-app="userApp"> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script> 
 
<script src="js/controllers/main.js"></script> 
 
</head> 
 
<body ng-controller="MainCtrl"> 
 
{{user}} 
 

 
</body> 
 
</html>

是否有人可以解釋爲什麼這不工作?

在Chrome JS控制檯我得到以下錯誤:

Uncaught Error: [$injector:nomod] Module 'userApp' 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.

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

回答

3

功能angular.module可以同時用作getter和setter:

你必須使用二傳手創建新模塊

angular.module('userApp', []); 

這裏的第二個參數是依賴關係陣列y

當你想訪問你現有的模塊, G。添加服務,控制器等,你可以使用getter(W/O第二PARAM)

angular.module('userApp') 

消氣將返回你的模塊,它可以應用生命週期中被調用多次。

0

我也很新的角度,但在聲明模塊沒有你需要的第二個參數?

angular.module('userApp', []); 

該解決方案是什麼樣的錯誤信息溝通:

If registering a module ensure that you specify the dependencies as the second argument.

+0

是的,當然可以,否則它是對現有的(以前聲明的)該名稱的模塊的引用 – charlietfl

+0

可以使用全局變量分配控制器W/O,但是您的第一條語句是真實的 –

+0

@ArtemPetrosian是的,您使用你有什麼問題...沒有依賴性參數...'angular.module('userApp').control('AnotherCtrl'...' – charlietfl

相關問題