2016-11-05 2 views
0

이 코드는 ngStorage를 사용합니다. 그러나 나는 이것이 내가 콘솔에이 오류가 내 각 응용 프로그램의 일부를 실행하려고하면알 수없는 공급자 : ngStorageProvider <- ngStorage Angular ngStorage를 사용하려고 시도하면

Error: [$injector:unpr] Unknown provider: ngStorageProvider <- ngStorage <- signupController 

이유는 무엇입니까? controller.js

에서

기호

var smallTalkzModel = angular.module('smallTalkzModel', ['ui.router', 'luegg.directives', 'ngCookies', 'ngStorage', 'angular-jwt']); 

smallTalkzModel.controller('signupController', ['$scope', '$location', '$http', 'userDetails','ngStorage', 
    function ($scope, $location, $http, userDetails,$localStorage) { 

     $scope.register_user = function (info) { 
      $http({ 
       url: '/register_user', 
       method: 'POST', 
       data: info 
      }).then(function (response) { 
       $localStorage.jwt = response.data.id_token; 
       $location.path('main'); 
      }, function (error) { 
       alert(error.data); 
      }); 
     } 
    }]); 

갱신 :

은 $ localStoragein에게 컨트롤러 paramters를 포함하도록 코드를 변경했습니다. 이제 오류 mesge이 사라,하지만 난 그것을로의 assigmnet을 한 후 $ 로컬 스토리지는 ... 정의되지

controller('signupController', ['$scope', '$location', '$http', 'userDetails', '$localStorage', 
    function ($scope, $location, $http, userDetails, $localStorage) { 

     $scope.login_info = ""; 
     $scope.userDetails = userDetails.isLogged; 
     $scope.userLogin = false; 
     $http.get('/online_users') 
      .success(function (data) { 
       $scope.usersNumber = data.length; 
      }) 
      .error(function (data) { 
       console.log('Error: ' + data); 
      }); 

     $scope.register_user = function (info) { 
      $http({ 
       url: '/register_user', 
       method: 'POST', 
       data: info 
      }).then(function (response) { 
       $localStorage.jwt = response.data.id_token; 
       $location.path('main'); 
      }, function (error) { 
       alert(error.data); 
      }); 
     } 

    }]); 

내 코드가 정의되지 않은 로컬 스토리지의 오류 던질 곳은 다음과 같습니다

mainController.js

smallTalkzModel.controller('mainController', ['$scope', 'sessionInfo', '$location', '$http', 'userDetails','$localStorage', 
    function ($scope, sessionInfo, $location, $http, userDetails, jwtHelper,$localStorage) { 

     $scope.login_info = ""; 
     $scope.userLogin = userDetails.isLogged; 

     var jwt = $localStorage.jwt; // here $localStorage is undefined 
.......... 

} 

답변

2

ngStorage를 설치하고 프로젝트에 추가해야합니다. 그것은 앵귤러 코어의 일부가 아닙니다.

이미 설치 한 경우 프로젝트에 스크립트를 추가하지 않아서 오류가 발생할 수 있습니다. 여기

공식 문서 : 여기 https://github.com/gsklee/ngStorage

예 : How to use ngStorage in angularjs

UPDATE : 나는 당신이 (문자열 부분에) ngStorage를 주입하는 signupController 컨트롤러에 비해 볼

때 injection은 $ localStorage 또는 $ sessionStorage 여야합니다. ngStorage가 공급자가 아닌 모듈이기 때문에 이로 인해 문제가 발생합니다.

업데이트 2 : 당신이 mainController, 두 번째 매개 변수를 선언 할 때

당신은 단지 $ 로컬 스토리지에 대한 문자열 전에 jwtHelper에 대한 문자열을 추가하는 것을 잊었다, 컨트롤러가, 의존하는 모듈입니다 . 그래서 당신의 함수에 하나의 String보다 적은 변수가 있습니다.

+0

이것이 확실하지 않은지 확인하십시오. npmstorage - save를 npm 설치 한 후 cdn 스크립트 src를 내 index.html에 추가했습니다. – Matoy

+0

설치 한 경우 cdn을 사용하지 마십시오. 파일에 대한 로컬 경로 만 사용하십시오. – fiso

+0

@Matoy, 내 업데이트를 확인해보십시오, 나는 대답이 있다고 생각합니다. – fiso

관련 문제