I'm trying to call function deleteConv
which is inside service ChatsService
from $ionicActionSheet, but it failed. I had an error Error: ChatsService.deleteConversation(...) is undefined
even though service is defined and injected controller
/**
Controller
**/
angular.module('Tot.controllers')
.controller('MessageController', function($scope, $timeout,ChatsService,$localStorage,,Globals,$ionicActionSheet,Messages) {
var iduser=$localStorage[Globals.USER_LOGGED].id;
$scope.onConversationHold = function(e, itemIndex, conversation) {
$ionicActionSheet.show({
cancelText:'<span class="no-text-transform">Annuler</span>',
destructiveText: '<span class="no-text-transform">Supprimer</span>',
destructiveButtonClicked: function() {
ChatsService.deleteConversation(conversation,iduser).then(function(response){
alert(response);
return true; //Close the model?
})
}
});
};
});
/**
ChatsService.js
**/
angular.module('Tot.services')
.service('ChatsService', function($q,$http,Globals) {
var url=Globals.urlServer+Globals.port;
this.deleteConversation=function(conversation,iduser){
var deferred=$q.defer();
$http.get(url+'/conversation/deleteConversation?idconversation='+conversation+'&iduser='+iduser).success(function(response){
if(response)
{
deferred.resolve(response);
}
});
}
});
How can I fix it ?
[EDITED]
/**
app.js
**/
angular.module('Tot', ['ionic','Tot.controllers','Tot.services','Tot.constants'])
.run(function($ionicPlatform,Messages,$rootScope,$cordovaStatusbar, $state,Globals,$localStorage,$mdDialog,$mdToast) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
//StatusBar.styleDefault();
$cordovaStatusbar.overlaysWebView(true);
$cordovaStatusbar.styleHex('#c62828')
}
....
})
okay so I am pretty sure you just need to inject the Tot.service module into your Tot.controller module. so the Tot.controller module should look like
angular.module('Tot.controllers', ['Tot.services'])
.controller('MessageController', function($scope, $timeout,ChatsService,$localStorage,,Globals,$ionicActionSheet,Messages) {
var iduser=$localStorage[Globals.USER_LOGGED].id;
$scope.onConversationHold = function(e, itemIndex, conversation) {
$ionicActionSheet.show({
cancelText:'<span class="no-text-transform">Annuler</span>',
destructiveText: '<span class="no-text-transform">Supprimer</span>',
destructiveButtonClicked: function() {
ChatsService.deleteConversation(conversation,iduser).then(function(response){
alert(response);
return true; //Close the model?
})
}
});
};
});
Similar to injecting ionic into your main module for you app. Modules have to be injected into other modules in order to access services and controllers inside those modules. That should work for you. let me know if it doesn't and I will have another look.