我有此服务:
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;
}
}
然后我在这样的控制器中使用它:
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);
}
}]
});
}
尽管它用于服务,但不能调用该方法,说不存在任何方法。
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
我缺少什么语法?
您没有返回服务中的任何内容。为了解决此问题,只需返回变量" AddSuggestedPeersService",那么您应该能够使用您的服务功能。
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;
}