什么是"装饰与QUOT;以及如何使用它们? [英] What are "decorators" and how are they used?
问题描述
我很好奇,究竟是什么装饰都在AngularJS。没有多少信息网上进行装饰保存在 AngularJS文档一Blurb的和短暂的(尽管有趣)提到在 YouTube视频。
I'm curious what exactly decorators are in AngularJS. There isn't much information online for decorators save for a blurb in the AngularJS documentation and a brief (albeit interesting) mention in a youtube video.
由于角的家伙放在一个装饰是:
As the Angular guys put it a decorator is:
服务的装饰,使装饰拦截服务
创建实例。返回的实例可以是原始实例,
或者一个新的实例,它代表到原来的实例。
Decoration of service, allows the decorator to intercept the service instance creation. The returned instance may be the original instance, or a new instance which delegates to the original instance.
我真的不知道这是什么的表示的,我不知道你为什么会这个逻辑从服务本身分开。例如,如果我想返回的东西在不同条件下不同的我只是通过不同的参数来实现相关功能或使用其他功能共享该私有状态。
I don't really know what that means, and I'm not sure why you would separate this logic from the service itself. For example if I wanted to return something different under different conditions I would just pass different arguments to the relevant functions or use another function sharing that private state.
我还是种类的AngularJS小白,所以我敢肯定,这只是无知和/或坏习惯,我拿起了。
I'm still kind of an AngularJS noob so I'm sure it's just ignorance and/or bad habits I've picked up.
推荐答案
一个很好的用例 $ provide.decorator
是当你需要做轻微的调整上一些第三方/上行业务,其上你的模块依赖,同时使服务不变(因为你不是服务的所有者/维护)。 这里是plunkr示范。
A good use case of $provide.decorator
is when you need to do minor "tweak" on some third-party/upstream service, on which your module depends, while leaving the service intact (because you are not the owner/maintainer of the service). Here is a demonstration on plunkr.
这篇关于什么是"装饰与QUOT;以及如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!