2015-09-20 90 views
0

我是新來的angularjs我想要創建一個登錄表單屬性「ClearCredentials」,但我得到以下錯誤:無法讀取的不確定

無法讀取屬性「ClearCredentials」的定義。

我已經使用了一些教程的東西。但是現在我開始從頭創建一個應用程序。但我堅持與AuthenticationService.js

這是我做了什麼

app.js

(function(angular) { 
    angular.module("app.directives", []); 
    angular.module("app.AuthenticationService", []); 
    angular.module("app.controllers", []); 
    angular.module("app", ['ngRoute','ngResource','routes','app.directives','app.controllers',"app.AuthenticationService"]); 
}(angular)); 

controller.js

//LoginController 
(function (angular) { 

var LoginController = function($scope,$location,AuthenticationService){ 

    var vm = this; 

    //todo vm.login = login; 

    (function initController() { 
     // reset login status 
     // calling function from AuthenticationService 
     AuthenticationService.ClearCredentials(); 
    })(); 


    /* TODO: This has not been tested yet 
    function login(){ 
     vm.dataLoading = true; 
     // constructor from AuthenticationService 
     AuthenticationService.Login(vm.username,vm.password, function(response){ 
      //Check if method respons === success 
      if (response.success) { 
       // calling function from AuthenticationService 
       AuthenticationService.SetCredentials(vm.username, vm.password); 
       $location.path('/'); 
      } else { 
       // use function from FlashService 
       // FlashService.Error(response.message); 
       // vm.dataLoading =false; 
      } 

     }); 
    }*/ 

} 

LoginController.$inject = ['$scope','$location']; 
angular.module("app.controllers").controller("LoginController", LoginController); 

}(angular)); 

AuthenticationService.js

(function (angular) { 


    var AuthenticationService = function($http, $cookieStore, $rootScope, $timeout, UserService) { 
     var service = {}; 

     service.ClearCredentials = ClearCredentials; 




     function ClearCredentials() { 
      $rootScope.globals = {}; 
      $cookieStore.remove('globals'); 
      $http.defaults.headers.common.Authorization = 'Basic '; 
     } 
    return service; 
    } 



    AuthenticationService.$inject = ['$http', '$cookieStore', '$rootScope', '$timeout', 'UserService']; 
    angular.module("app.AuthenticationService").factory("AuthenticationService", AuthenticationService); 

})(angular); 

如果您對此代碼有任何建議,請讓我也知道。

回答

1

可以注入兩個參數:

LoginController.$inject = ['$scope','$location']; 

但後來你指的是第三個。這不是問題嗎?

檢查內嵌版本將工作:

angular.module("app.controllers").controller("LoginController", funtion($scope,$location,AuthenticationService){ 
... 
} 

如果確實如此,那麼,這是角注入的問題。

+0

偉大的我得到它的工作,這確實是角注射問題。我添加了AuthenticationService,現在它工作正常。 – Greg