Angularjs 如何在$IoniActionSheet中注入服务

Angularjs 如何在$IoniActionSheet中注入服务,angularjs,angularjs-scope,ionic-framework,Angularjs,Angularjs Scope,Ionic Framework,我试图调用函数deleteConv,它是$ionicActionSheet中的内部服务chatservice,但失败了。我有一个错误错误:ChatsService.deleteConversation(…)未定义,即使服务已定义并已注入控制器 /** Controller **/ angular.module('Tot.controllers') .controller('MessageController', function($scope, $tim

我试图调用函数
deleteConv
,它是$ionicActionSheet中的内部服务
chatservice
,但失败了。我有一个错误
错误:ChatsService.deleteConversation(…)未定义
,即使服务已定义并已注入控制器

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

好的,我很确定您只需要将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?

                        })
                    }
                });
            };

    });
angular.module('Tot.controllers',['Tot.services']))
.controller('MessageController',函数($scope、$timeout、ChatsService、$localStorage、、Globals、$IoniActionSheet、Messages){
var iduser=$localStorage[Globals.USER\u LOGGED].id;
$scope.onConversationHold=函数(e、itemIndex、对话){
$IoniActionSheet.show({
取消文本:'Canceler',
destructiveText:“Supplimer”,
destructiveButtonClicked:function(){
deleteConversation(conversation,iduser).then(函数(响应){
警报(响应);
返回true;//是否关闭模型?
})
}
});
};
});

类似于将ionic注入应用程序的主模块。必须将模块注入其他模块,以便访问这些模块内的服务和控制器。那应该对你有用。如果没有,请告诉我,我会再看一眼。

好的,我很确定您只需要将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?

                        })
                    }
                });
            };

    });
angular.module('Tot.controllers',['Tot.services']))
.controller('MessageController',函数($scope、$timeout、ChatsService、$localStorage、、Globals、$IoniActionSheet、Messages){
var iduser=$localStorage[Globals.USER\u LOGGED].id;
$scope.onConversationHold=函数(e、itemIndex、对话){
$IoniActionSheet.show({
取消文本:'Canceler',
destructiveText:“Supplimer”,
destructiveButtonClicked:function(){
deleteConversation(conversation,iduser).then(函数(响应){
警报(响应);
返回true;//是否关闭模型?
})
}
});
};
});

类似于将ionic注入应用程序的主模块。必须将模块注入其他模块,以便访问这些模块内的服务和控制器。那应该对你有用。如果没有,请告诉我,我会再看一看。

非常确定您应该将您的服务注入您的模块,您可以制作一个代码笔或发布更多代码吗?@JessPatton我用整个控制器和服务更新了我的问题。我确定您应该将您的服务注入您的模块,你能做一个代码笔或发布更多的代码吗?@JessPatton我用整个控制器更新了我的问题,服务
Tot.service
已经注入
app.js
,正如你在我的问题(编辑)中看到的那样因此,我认为没有必要在Tot.controllers中再次注入它。试一下,告诉我发生了什么,然后我再看一次。如果没有成功,我想提醒一下,ChatsService中的其他函数调用Tot的$ionicActionSheet之外的函数。控制器运行没有问题。你能做一个代码笔吗?或者把你的代码放在github上,这样我就可以抓取它并调试一下?
Tot.service
已经被注入了
app.js
,正如你在我的问题(编辑)中看到的那样,所以我认为没有必要在
Tot.controllers
中再次注入它,那么试试吧,告诉我会发生什么,然后我会再次看到它成功,我想提醒一下,ChatsService中的其他函数从Tot.Controllers调用$ionicActionSheet之外的函数。运行时没有问题。你能做一个代码笔吗?或者只是把你的代码放在github上,这样我就可以抓取它并调试一下?