是否可以从angularjs中的服务访问$ cookieStore?

Bru*_*iro 2 javascript angularjs

我正试图$cookieStore.get("username")从这样的服务中访问$ cookieStore():

var angServices = angular.module('angServices', ['ngResource', 'ngCookies']);
angServices.factory('rsrUser', [ '$resource',
  function($resource, $cookieStore){
    var req = "/login"
    var timestamp = getMicrotime(true).toString();
    var username = $cookieStore.get("username");
    return $resource(baseURL + req, {}, {
      logIn: {method:'POST',
              isArray:false,
              headers:{
                  'X-MICROTIME': timestamp,
                  'X-USERNAME': username,
                  'X-HASH': getHMAC(timestamp, req)
                }
            }
    });
  }]);
Run Code Online (Sandbox Code Playgroud)

但这会回来 TypeError: Cannot call method 'get' of undefined

我做错了什么或者为什么要做到这一点更好?

Dav*_*her 5

您正在使用带注释的注入,但不为cookie存储提供注释.

var angServices = angular.module('angServices', ['ngResource', 'ngCookies']);

// Add $cookieStore to the array,             here --\
angServices.factory('rsrUser', [ '$resource', '$cookieStore'

  function($resource, $cookieStore){
    // the same
  }
}]);
Run Code Online (Sandbox Code Playgroud)