Search code examples
javascriptangularjsdependency-injectionjwtangularjs-factory

Can't seem to inject angular-jwt into factory


I am trying to inject angular-jwt into a factory for use in auth functions, but I keep getting the error

`Error: [ng:areq] Argument 'fn' is not a function, got string http://errors.angularjs.org/1.3.15/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20string

return new ErrorConstructor(message);`

Here's the code: webapp.factory('Auth', ['angular-jwt'], function($http, API_URL, $window, $location, jwtHelper ) {

Also the learning curve to angular seems sharp in this way, how am I supposed to make sense out of the error to find out why it's failing? Thanks!


Solution

  • Inject all the required dependency in your app module. That what you were injecting new module while declaring factory.

    Code

    var webapp = angular.module('myAppName',['angular-jwt']);
    
    webapp.factory('Auth',['$http', 'API_URL', '$window', '$location', 'jwtHelper', 
       function($http, API_URL, $window, $location, jwtHelper ) {
       //your c0de here
    }]);