Dennis
Dennis

Reputation: 368

Activate service function on click

I have a click function what needs to activate a function and pass some data in, but for now I'm only trying to get the function work in my service but I keep getting 'is not a function' and it drives me a little crazy since I can't figure out why.

My click where I call the services and the function

    scope.click = function() {
         infoService.method();
    };

The function himself.

app.service('infoService', function() {

    function method() {
        return alert('hello world!');
    };

});

Upvotes: 1

Views: 34

Answers (2)

jsonmurphy
jsonmurphy

Reputation: 1600

Try this:

app.service('infoService', function() {

    this.method = function () {
        return alert('hello world!');
    };

});

The issue here is that you've declared the internal function but its currently only accessible privately (within the service).

Upvotes: 1

try this:

this.method = function(){
   return alert('hello world!');
}

Make sure that infoService is injected into your controller also.

Upvotes: 1

Related Questions