如何从工厂发出事件 [英] how to emit events from a factory

查看:34
本文介绍了如何从工厂发出事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从工厂或服务发出事件.我无法将 $scope 注入工厂,因此无法发出事件.

How can I emit events from a factory or service. I am unable to inject $scope into the factory, thus unable to emit events.

我收到以下错误 - Unknown provider: $scopeProvider <- $scope

I get the following error - Unknown provider: $scopeProvider <- $scope

谢谢,穆尔塔扎

推荐答案

您不能将控制器的作用域注入到服务中.你可以做的是:

You cannot inject a controller's scope into a service. What you can do is:

  • 将范围实例作为参数传递给您的服务功能之一:

例如

app.factory('MyService', function() {

   return {
      myFunction: function(scope) {
         scope.$emit(...);
         ...
      }
    };
});

  • 将 $rootScope 注入您的服务:
  • 例如

    app.factory('MyService', ['$rootScope', function($rootScope) {
    
       return {
          myFunction: function() {
             $rootScope.$emit(...);
             ...
          }
        };
    }]);
    

    这篇关于如何从工厂发出事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆