2015-08-14 34 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