Sangeetha.V
Sangeetha.V

Reputation: 115

unable to set or get a method from service throwing not a function issue in angularjs

In my service:

 myApp.service('settingsService', [
       function() {
         var id;
          this.setObjectId = function (data)
          {
            id = data;
          }
          this.getObjectId = function ()
          {
             return id;
           }
}]);

From my directive: Note: Already injected that service.

settingsService.setObjectId(4); am setting like this

But am getting the following script error:

TypeError: undefined is not a function

If any one knows means please upadte. Thanks in advance

Upvotes: 0

Views: 31

Answers (1)

DonJuwe
DonJuwe

Reputation: 4563

Try to set up your service like this:

myApp.service('settingsService', function() {
    var id;
    var setObjectId = function(data) {
        id = data;
    }
    var getObjectId = function() {
        return id;
    }

    return {
        setObjectId: setObjectId,
        getObjectId: getObjectId
    };
});

Upvotes: 1

Related Questions