简体   繁体   English

装饰和访问$ rootScope方法

[英]decorating and accessing $rootScope method

I have decorated $rootScope in the config of module with a fn safeApply . 我已经用fn safeApply装饰了模块配置中的$ rootScope。

$provide.decorator('$rootScope', ['$delegate', function($delegate) {
        $delegate.safeApply = function(fn) {
           ...
         };
         return $delegate;
         }
]);

Is it ok to access it like 可以像这样访问它吗

 $scope.$root.safeApply();

or do I need to inject $rootScope and then call it. 还是我需要注入$rootScope然后调用它。

Can I add this method to the prototype of $rootScope so it will inherit in all $scope ? 我可以将此方法添加到$rootScope的原型中,以便它将在所有$scope继承吗? How to do this. 这个怎么做。

Edit 编辑

with khanh's response below, I may add bit more info. 以下是khanh的回应,我可能会添加更多信息。 safeApply is a method used to trigger a digest cycle manually. safeApply是一种用于手动触发摘要周期的方法。 The idea of decorating the $rootScope is from here https://coderwall.com/p/ngisma . 装饰$ rootScope的想法是从这里https://coderwall.com/p/ngisma Please see it in the comment. 请在评论中看到它。 So, it is not decorating a method, more adding a functionality which is accessible across (scope of directive, controller) 因此,它不是在修饰方法,而是增加了可跨(指令范围,控制器范围)访问的功能。

Decorators should be used with services instead of scopes. 装饰器应与服务而不是范围一起使用。

Decorator is a mechanism that allows use to wrap original service and performs cross-cutting concerns like: caching, logging,.. and leave the service intact to focus on its business without being polluted by these code. Decorator是一种机制,它允许使用它包装原始服务并执行横切关注点,例如:缓存,日志记录.., 并使服务完整无损地专注于其业务,而不会受到这些代码的污染

I would implement the decorator like this. 我会像这样实现装饰器。 Let's say, we have a service defined in another module: 假设我们在另一个模块中定义了一个服务:

var lib = angular.module("lib",[]);
lib.service("util",function(){
     this.saveFile = function(file){
       console.log("save file:" + file);
     }
});

We need to use this service and decorate it with our code to do some logging without polluting the service with logging logic: 我们需要使用此服务,并用我们的代码装饰它以进行一些日志记录,而不会使用日志记录逻辑污染该服务:

app.config(function($provide) {
  $provide.decorator('util', function($delegate) {

    var originalSaveFile = $delegate.saveFile;

    $delegate.saveFile = function(file){
       console.log("before save file");
       originalSaveFile.apply(this,arguments);
       console.log("after save file");
    }

    return $delegate;
  });
});

DEMO DEMO

Decorator is inspired from decorator pattern and aspect oriented programming 装饰器的灵感来自装饰器模式面向方面的编程

In your case, you could just add the function to $rootScope in module's run block and all scopes will inherit the function. 在您的情况下,您可以仅将该函数添加到模块的run块中的$ rootScope中,所有作用域都将继承该函数。

app.run(function($rootScope){
  $rootScope.safeApply = function(fn){
      console.log("safeApply");
  };
});

DEMO DEMO

We could also use decorator to do something like this, but it feels like it's not the right way as the idea of decorators is to create wrappers : 我们也可以使用decorator来做类似的事情,但是感觉这不是正确的方法,因为decorator的想法是创建包装器

$provide.decorator('$rootScope', function($delegate) {

    $delegate.safeApply = function(fn){
      console.log("safe apply");
    }
    return $delegate;
  });

DEMO DEMO

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

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