2017-11-25 113 views
1

我有一個簡單的前端應用程序,可以找到here。在我的索引文件我加載app.js先authentication.js服務:無法在AngularJS中註冊模塊

<script src="scripts/app.js"></script> 
<script src="scripts/services/authentication.js"></script> 

認證模塊可以在scripts/services/authentication.js文件中找到,看起來像這樣:

'use strict'; 

angular.module('Authentication') 

.factory('AuthenticationService', 
    ['$http', '$rootScope', 
    function ($http, $rootScope) { 
     var service = {}; 

     service.Login = function (username, password) { 
      $http.post('/users/authenticate', { username: username, password: password }) 
       .success(function (response) { 
        console.log(response); 
       }); 
     }; 

     service.SetCredentials = function (token) { 
      $rootScope.localStorage.setItem('token', token);    
      $http.defaults.headers.common['Authorization'] = 'Bearer ' + token; // jshint ignore:line 
     }; 

     service.ClearCredentials = function() { 
      $rootScope.removeItem('toke'); 
      $http.defaults.headers.common.Authorization = 'Bearer '; 
     }; 

     return service; 
}]); 

我非常新AngularJS和我只是遵循這個tutorial。我運行我的應用程序時遇到的問題是:

模塊「身份驗證」不可用。你要麼拼寫錯誤的模塊名稱或忘了裝載它...

+0

我試圖在index.html文件中交換app.js和authentication.js,但得到了'Module frontendApp is not available'。我真的很想知道,爲什麼這樣一個基於教程的簡單代碼不起作用。 – Jacobian

回答

2
angular.module('Authentication') 

應該

angular.module('app') 

因爲你嘗試加載在同一文件中的兩個模塊,但根據這個可能的,即使你的教程在這裏提到那種事情不能這樣做,將Authentication更改爲app

+0

我會在一秒鐘內檢查它! – Jacobian

+0

它的工作原理!謝謝你,先生! – Jacobian

+0

哦,對不起!謝謝,小姐! – Jacobian