0

I have this Service:

function AddSuggestedPeersService($http, SITE_CONFIG) {
    console.log("I come in Add Suggested Peers Service");

    var addSuggestedPeers = this;

    addSuggestedPeers.addSuggPeer = function(peerID, mode) {
        // a service
        return response;
    }
}

Then I use this in the controller like this:

peerHealthController.AddConfirm = function(peerID,mode,peerName){
        console.log("I come in Add Confirm");
        console.log("Peer ID =====" + peerID);
        var confirmPopup = $ionicPopup.confirm({
          title: '',
          cssClass : 'confirmBox',
          templateUrl: 'peers/add_peer_confirmation.template.html',
          buttons: [{
             text: 'NO, THANKS'
          }, {
             text: '<b>YES, PLEASE</b>',
             type: 'button-yes',
             onTap: function(e) {
               e.preventDefault();
               return AddSuggestedPeersService.addSuggPeer(peerID,mode);
             }
          }]
        });

      }

Though it goes to the service but cannot call the method, saying no method exists.

ionic.bundle.js:26794 TypeError: AddSuggestedPeersService.addSuggPeer is not a function
    at Object.onTap (peersLandingPage.controller.js:108)
    at ChildScope.$buttonTapped (ionic.bundle.js:56771)
    at fn (eval at compile (ionic.bundle.js:27638), <anonymous>:4:386)
    at ionic.bundle.js:65427
    at ChildScope.$eval (ionic.bundle.js:30395)
    at ChildScope.$apply (ionic.bundle.js:30495)
    at HTMLButtonElement.<anonymous> (ionic.bundle.js:65426)
    at defaultHandlerWrapper (ionic.bundle.js:16787)
    at HTMLButtonElement.eventHandler (ionic.bundle.js:16775)
    at triggerMouseEvent (ionic.bundle.js:2953)(anonymous function) @ ionic.bundle.js:26794

What syntax am I missing?

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108

1 Answers1

0

You are not returning anything in your service. To fix this simply return the variable 'addSuggestedPeersService' then you should be able to use the functions from you service.

function AddSuggestedPeersService($http, SITE_CONFIG) {
console.log("I come in Add Suggested Peers Service");

var addSuggestedPeers = this;

addSuggestedPeers.addSuggPeer = function(peerID, mode) {
    // a service
    return response;
}
return addSuggestedPeers;
}
Umer Z
  • 200
  • 10
  • Nope. Does not work. Same error. I already had the return response. Added the other return as well, but does not work. – Pritam Banerjee Dec 08 '16 at 22:07
  • Can you post the declarations of your controller, where your injecting your dependencies? – Umer Z Dec 08 '16 at 22:14
  • I got the code in controller and fixed it. Could not get it from the service. – Pritam Banerjee Dec 09 '16 at 01:09
  • Can you post your declaration of the controller, it is where you state all your dependencies. – Umer Z Dec 09 '16 at 15:39
  • Injecting the service was fine. AddSuggPeer service is called. But not the method inside that Service. – Pritam Banerjee Dec 09 '16 at 19:37
  • it should look something like this: someModule.controller('MyController', ['$scope', 'dep1', 'dep2', function($scope, dep1, dep2) { – Umer Z Dec 09 '16 at 19:39
  • Yeah, I already have that. Service is called. But not the function inside that service. Somehow it is not recognized. Has to do something with the `onTap` functionality of Ionic framework. Same methodology works for other calls but not for `onTap`. – Pritam Banerjee Dec 09 '16 at 19:41