Christian Boyer
Christian Boyer

Reputation: 48

Angular factory "is not a constructor"

I'm trying to create a factory but am getting an error instantiating it. Am I missing something?

sessionFactory is not a constructor

at new sessionController (sessionController.js:5)

Controller

angular.module('app').controller('sessionController', ['sessionService', 'sessionFactory', sessionController]);

function sessionController(sessionService, sessionFactory) {
    var vm = this;
    var mySessionFactory = new sessionFactory();
}

Factory

angular.module('app').factory('sessionFactory', ['$window', sessionFactory]);
function sessionFactory($window) {
    return {
        save: save,
        get: get,
        clear: clear
    }

    function save(key, value) {
        $window.sessionStorage.setItem(key, value);
    }

    function get(key) {
        return $window.sessionStorage.getItem(key);
    }

    function clear() {
        $window.sessionStorage.clear();
    }
}

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script src="./app/app.js"></script>
<script src="./app/sessionController.js"></script>
<script src="./app/sessionService.js"></script>
<script src="./app/sessionFactory.js"></script>

Upvotes: 0

Views: 2527

Answers (4)

AkashPushPopStack
AkashPushPopStack

Reputation: 23

You can use the new sessionFactory(), the sessionFactory have to return a function rather than an object to be used as a Constructor. So you can return an anonymous function which would return an object.

 function sessionFactory($window){

    function save(key, value) {
        $window.sessionStorage.setItem(key, value);
    }

    function get(key) {
        return $window.sessionStorage.getItem(key);
    }  

    function clear() {
        $window.sessionStorage.clear();
    }
    return function () {
        return {
            save : save,
            get : get,
            clear : clear
        }
    }
}

This implementation will work fine.

Upvotes: 0

Vandesh
Vandesh

Reputation: 6894

If you want to create an instance of a factory, you should return a function as follows -

angular.module('foo', [])
.factory('Foo', ['$http', function($http) {
  return function() {
    this.x = "Some";
    this.y = "value";

    this.someFunction = function() {
      return this.x + this.y;
    };
  };
}]);

And then you can instantiate it using -

var factoryObj = new Foo();

If you're returning an object instead of a (constructor) function , then no need to instantiate an object using new. You can directly use sessionFactory

Upvotes: 1

nabukhas
nabukhas

Reputation: 193

Here is the problem with this line

 var mySessionFactory = new sessionFactory();

you don't have to instantiate it just use it directly because it already instantiate.

Upvotes: 1

Frank Modica
Frank Modica

Reputation: 10526

Angular calls the following factory function once to get a singleton to be used for injection:

function sessionFactory($window) {
    return {
        save: save,
        get: get,
        clear: clear
    }

    // ...
}

So you are calling new on this object:

{
    save: save,
    get: get,
    clear: clear
}

You don't have to call new since you already have the object you need.

Upvotes: 2

Related Questions