2015-08-14 43 views
1

我正在尝试调用函数deleteConv,该函数在$ ionicActionSheet中服务ChatsService中,但失败。我有一个错误Error: ChatsService.deleteConversation(...) is undefined即使服务定义和注入控制器

/** 
    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); 
       } 
      }); 
     } 
    }); 

我怎样才能解决呢?

将帖子

/** 
    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') 

      } 
     .... 
     }) 
+0

很确定你应该注入你的服务到你的模块中,你可以做一个codepen或张贴更多的代码吗? –

+0

@JessPatton我用整个控制器和服务更新了我的问题 – Pisix

回答

0

好了,所以我敢肯定,你只需要在Tot.service模块注入到你的Tot.controller模块。所以Tot.controller模块应该看起来像

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? 

         }) 
        } 
       }); 
      }; 

    }); 

类似为您的应用程序注入离子到您的主要模块。模块必须注入其他模块才能访问这些模块内的服务和控制器。这应该适合你。让我知道如果它不,我会再看看。

+0

''Tot.service'已经注入'app.js',正如你在我的问题(已编辑)中看到的,所以我认为没有必要在' Tot.controllers' – Pisix

+0

试试吧,告诉我会发生什么,然后我再看看它 –

+0

没有成功,我想提醒别人在ChatsService调用函数之外从Tot.Controllers $ ionicActionSheet运行没有问题 – Pisix