简体   繁体   English

为什么服务不接收事件

[英]why a service does not receive events

I have some code that was working in a controller.我有一些在控制器中工作的代码。 I refactored and moved that functionality into a service.我重构并将该功能移动到服务中。 The code had an event listener:该代码有一个事件侦听器:

$rootScope.$on( .....)

This was caught when it was in the controller, but does not catch the event when in a service.这在控制器中时被捕获,但在服务中时不会捕获事件。 I have tried using $rootScope.$broadcast() and $rootScope.$emit() to signal the event but neither are caught in the service.我曾尝试使用$rootScope.$broadcast()$rootScope.$emit()来发出事件信号,但都没有被服务捕获。

There is nothing special about the service code:服务代码没有什么特别之处:

heartbeatService.$inject = [ '$rootScope', 'vcapChanService', 'eventService', 'logger' ];
function heartbeatService( $rootScope, vcapChanService, eventService, logger ) {
    //....
}

Question: how do I get a service to catch events on the root scope?问题:如何获取服务以捕获根作用域上的事件?

Edit with more info: The event is signaled from another service.使用更多信息进行编辑:该事件是从另一个服务发出的。 And this event is caught in other controllers, but is NOT caught in another service.并且此事件在其他控制器中被捕获,但不会在其他服务中被捕获。 I have tried all variations of service/factory, emit/broadcast, etc. and none of them work.我已经尝试了服务/工厂、发射/广播等的所有变体,但它们都不起作用。 The service just does not catch the event but in all other regards works perfectly.该服务只是没有赶上事件,但在所有其他方面都完美无缺。

you can make service bridge between them and use watch您可以在它们之间建立服务桥梁并使用 watch

like喜欢

// bridge service
angular.module('app').service('bridge', function() {
        var self = this;
        self.events = [];
        self.fireEvent = function(eventName) {
            self.events.push(eventName);
        }

        self.removeEvent = function(eventName) {
            var ps = self.events.indexOf(eventName);
            if (ps != -1)
                self.events.splice(ps, 1) // to remove the event from events array 
        }
    })
    // firstService
angular.module('app').service('firstService', function($rootScope, bridge) {
        // make your business logic here 

        $rootScope.$watchCollection(function() {
            return bridge.events
        }, function(values) {
            var event = 'fileUploaded';
            if (values != null && values.indexOf(event)) {
                // make what you need to when the event fired
                // then remove the event from the the service 
                bridge.removeEvent(event)
            }
        })

    })
    // secondService
angular.module('app').service('secondService', function($rootScope, bridge) {
        var self = this;
        // make your business logic here 
        self.uploadFile = function(file) {
            bridge.fireEvent('fileUploaded');
        }
    })
    // controller
angular.module('app').controller('Appcontroller', function(secondService, $scope) {
    $scope.clickToUpload = function() {
        secondService.uploadFile( /* file */ );
    }


})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM