如何从工厂发出事件

时间:2020-12-06 00:03:59

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

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

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

我得到以下错误-未知提供程序:$scopeProvider <- $scope

Thanks, Murtaza

谢谢,穆尔塔扎

3 个解决方案

#1


55  

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

不能将控制器的范围注入到服务中。你能做的是:

  • pass the scope instance as a parameter to one of your service functions:
  • 将范围实例作为参数传递给您的服务函数之一:

e.g.

如。

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

   return {
      myFunction: function(scope) {
         scope.$emit(...);
         ...
      }
    };
});
  • inject the $rootScope into your service:
  • 将$rootScope注入您的服务:

e.g.

如。

app.factory('MyService', ['$rootScope', function($rootScope) {

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

#2


54  

Inject $rootScope instead of $scope and then emit it on the $rootScope.

注入$rootScope而不是$scope,然后在$rootScope上发出它。

myApp.factory('myFactory', ['$rootScope', function ($rootScope) {
    $rootScope.$emit("myEvent", myEventParams);
}]);

Factories don't have access to the current controller/directive scope because there isn't one. They do have access to the root of the application though and that's why $rootScope is available.

工厂没有访问当前控制器/指令范围的权限,因为没有。但是,它们确实可以访问应用程序的根,这就是为什么可以使用$rootScope的原因。

#3


-1  

In your factory inject $rootScope as-

在您的工厂中注入$rootScope作为-

myApp.factory('myFactory',function($rootScope){
return({
// use $rootScope as below to pass myEventParams to all below in hierarchy
$rootScope.$broadcast("myEvent",myEventParams);

})
}]);

#1


55  

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

不能将控制器的范围注入到服务中。你能做的是:

  • pass the scope instance as a parameter to one of your service functions:
  • 将范围实例作为参数传递给您的服务函数之一:

e.g.

如。

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

   return {
      myFunction: function(scope) {
         scope.$emit(...);
         ...
      }
    };
});
  • inject the $rootScope into your service:
  • 将$rootScope注入您的服务:

e.g.

如。

app.factory('MyService', ['$rootScope', function($rootScope) {

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

#2


54  

Inject $rootScope instead of $scope and then emit it on the $rootScope.

注入$rootScope而不是$scope,然后在$rootScope上发出它。

myApp.factory('myFactory', ['$rootScope', function ($rootScope) {
    $rootScope.$emit("myEvent", myEventParams);
}]);

Factories don't have access to the current controller/directive scope because there isn't one. They do have access to the root of the application though and that's why $rootScope is available.

工厂没有访问当前控制器/指令范围的权限,因为没有。但是,它们确实可以访问应用程序的根,这就是为什么可以使用$rootScope的原因。

#3


-1  

In your factory inject $rootScope as-

在您的工厂中注入$rootScope作为-

myApp.factory('myFactory',function($rootScope){
return({
// use $rootScope as below to pass myEventParams to all below in hierarchy
$rootScope.$broadcast("myEvent",myEventParams);

})
}]);