Angular Factory, Service, and Controller not Working Together with $Resource

114 views Asked by At

I have an Angular service that's just a setter and getter for a variable I need to pass between controllers:

  var MyApp = angular.module('MyApp', ['ngResource', 'ngRoute'])
    .service('userToken', function() {
      var userTokenProp;
      return {
        getter: function() {
          return userTokenProp;
        },
        setter: function(value) {
          userTokenProp = value;
        }
      };
    })

Then a bit further down I'm trying to use $resource in a factory based on the value of that token. There's another almost identical factory that is able to set the token, but this latter factory can't get or use the token. I also need to send the token into the header while making the request. The code I have for that is this, a form I settled on after trying any number of other ways to pass the data into the factory and to set the header in $resource:

.factory("apiQueryProfileFactory", function($resource, userToken) {
      console.log("token: " + userToken.getter());
      var uToken = userToken.getter();
      var resource = $resource('http://localhost:3001/lsportal/api/v1/profile/:path', {}, {
        get: {
          method: "GET",
          headers: {'token': uToken}
        },
      });
      return resource;
    });

And then from the controller (which, yes, is passed the factory and service):

apiQueryProfileFactory.get({path:'role'}, function(data) {

})

Just don't get what I'm missing, and I've been over and over it for a while now!

0

There are 0 answers