1

I have the following angular service.

angular.module('myApp')
.service('StatusService', function StatusService() {
    var statusService= {
        show: 'off',
        on: function() {
            this.show = 'on';
        },
        off: function() {
            this.show = 'off';
        }
    };

    return statusService;
});

Which is injected into a controller, and its on function is invoked, like this:

angular.module('myApp')
    .controller('aController', function (StatusService) {

    StatusService.on(); 
})

But I get the following error.

TypeError: Object #<Object> has no method 'on'

2 Answers 2

1

You will get this problem if you declared multiple service with the same name in different places, it got confused and will return an {}.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you're using a module.service() invocation with module.factory() implementation when declaring the service. either change the earlier to module.factory(StatusService) (which should really be called StatusServiceFactory) OR change impl to add interface methods to StatusService itself ie:

function StatusService(){
this.show='off';
this.on = function(){};
}
module.service('statusService', StatusService);

Here's a demo of the above working: http://plnkr.co/edit/r07kPP85oQQKtJYMYOyK?p=preview

1 Comment

I changed the type of service to a factory, but the same issue is still occurring. I have always used the same implementation, using service('aservice'), and I have been able to use the service to store data. Basically I just want to be able to change the show to 'on' or 'off' using the methods on() and off().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.