如何在$ ionicActionSheet中注入服务

时间:2015-08-14 19:17:49

标签: angularjs angularjs-scope ionic-framework

我试图从$ ionicActionSheet调用服务deleteConv内的函数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);
                }
            });
        }
    });

我该如何解决?

[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')

            }
        ....
        })

1 个答案:

答案 0 :(得分: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?

                        })
                    }
                });
            };

    });

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