2015-07-19 49 views
0

儘管他沒有提供有關提供者的任何規則,但我仍使用John Papa的風格指南編寫了一個提供者。當我在我的應用程序的運行程序段注入我的服務我得到以下錯誤:使用John Papa的風格指南語法編寫的提供者的縮小問題

Uncaught Error: [$injector:strictdi] auth is not using explicit annotation and cannot be invoked in strict mode

運行塊:

angular.module('app', ['rest-client']) 
.run(runBlock); 

runBlock.$inject = ['Restangular', '$http', 'auth']; 

/* @ngInject */ 
function runBlock(Restangular, $http, auth) { 
} 

我的供應商:

(function() { 
    'use strict'; 

    angular 
     .module('rest-client') 
     .provider('auth', authProvider); 

    authProvider.$inject = []; 

    /* @ngInject */ 
    function authProvider() { 

     var config = { 
      authBaseUrl: undefined 
     }; 

     var serviceProvider = { 
      setAuthBaseUrl: setAuthBaseUrl, 
      $get: $get 
     }; 

     return serviceProvider; 

     function setAuthBaseUrl(authBaseUrl) { 
      config.authBaseUrl = authBaseUrl; 
     }; 

     $get.$inject = ['localStorageService', 'Restangular', '$q', '$http']; 

     /* @ngInject */ 
     function $get(localStorageService, Restangular, $q, $http) { 
      return authService(localStorageService, Restangular, $q, $http); 
     }; 

     authService.$inject = ['localStorageService', 'Restangular', '$q', '$http']; 
     /* @ngInject */ 
     function authService(localStorageService, Restangular, $q, $http) { 

      var service = { 
       getAccessToken: getAccessToken, 
       setAccessToken: setAccessToken, 
       getRefreshToken: getRefreshToken, 
       setRefreshToken: setRefreshToken, 
       deleteTokens: deleteTokens, 
       logout: logout, 
       refreshToken: refreshToken 
      }; 

      return service; 

      //////////////// 

      function getAccessToken() { 
       var token = localStorageService.get('access-token'); 
       return token; 
      } 

      function setAccessToken(token) { 
       localStorageService.set('access-token', token); 
       Restangular.setDefaultHeaders({Authorization: 'Bearer ' + this.getAccessToken()}); 
      } 

      function getRefreshToken() { 
       var token = localStorageService.get('refresh-token'); 
       return token; 
      } 

      function setRefreshToken(token) { 
       localStorageService.set('refresh-token', token); 
      } 

      function deleteTokens() { 
       localStorageService.remove('access-token'); 
       localStorageService.remove('refresh-token'); 
      } 

      function logout() { 
       localStorageService.clearAll(); 
       Restangular.setDefaultHeaders({Authorization: 'Bearer ' + this.getAccessToken()}); 
      } 

      function refreshToken() { 
       var deferred = $q.defer(); 
       $http.get(config.authBaseUrl + '/auth/refresh/' + this.getRefreshToken()).then(function (result) { 
        var accessToken = result.data.accessToken; 
        this.setAccessToken(accessToken); 
        deferred.resolve(accessToken); 
       }); 
       return deferred.promise; 
      } 

     } 

    } 
})(); 

回答

0
return serviceProvider; 

之前運行

$get.$inject = ['localStorageService', 'Restangular', '$q', '$http']; 

authService.$inject = ['localStorageService', 'Restangular', '$q', '$http']; 

後者是不必要順便說一句,它不會在DI參加。