服务,指令和模块之间有什么区别? [英] What is the difference between service, directive and module?

查看:64
本文介绍了服务,指令和模块之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多文档,并且越来越感到困惑. 我基本上无法弄清

I have been reading a lot of docs, and I'm getting more and more confused. I basically can't figure out the difference between a

  • 服务
  • 指令
  • 模块

我看到了很多自定义组件.有时他们使用指令,有时使用服务.它总是以模块开头.有人可以举例说明这三种类型之间的区别吗?

I see a lot of custom components. Sometimes they're using directives, sometimes services. It always starts with a module. Can someone explain with an example what the difference is between these three types?

推荐答案

将模块看作是连接许多其他东西的地方,例如指令,服务,常量等.可以将模块注入到其他模块中为您提供高水平的重用性.

Think of a module as being a place to wire up a number of other things, such as directives, services, constants etc. Modules can be injected into other modules giving you a high level of reuse.

编写有角度的应用程序时,您将拥有一个顶级模块,即您的应用程序代码(没有模板).

When writing an angular app, you would have a top-level module which is your application code (without templates).

服务主要是在控制器之间进行通信的一种方式,但是您可以将一个服务注入另一个服务.服务通常被用作访问数据存储的一种方式,人们会包装一些角度化的API,例如ngResource.该技术非常有用,因为它使测试(尤其是模拟)变得非常容易.您可以使用服务来执行其他操作,例如身份验证,日志记录等.

Services are mainly a way to communicate between controllers, but you can inject one service into another. Services are often used as a way to get to your data stores and people will wrap the angular APIs, such as ngResource. This technique is useful since it makes testing (particularly mocking) quite easy. You can have services for doing other things like authentication, logging etc.

伪指令用于创建窗口小部件或包装诸如jquery插件之类的现有内容.包装现有的插件可能是一个挑战,而这样做的原因是在插件和angular之间建立双向数据绑定.如果不需要双向数据绑定,则不需要将它们包装.

Directives are used for creating widgets or wrapping existing things like jquery plugins. Wrapping existing plugins can be a challenge and the reason you would do this is to establish a two-way data binding between the plugins and angular. If you don't need two-way data binding then you don't need to wrap them.

指令也是进行DOM操作,捕获DOM事件等的地方.您不应在控制器或服务中进行与DOM相关的工作.创建指令可能会变得非常复杂.恕我直言,我建议您首先查看可以满足您需求的API,或者向Angular的Google小组寻求建议.

A directive is also a place for doing DOM manipulation, catching DOM-events etc. You should not be doing DOM-related stuff in controllers or services. Creating directives can get pretty complex. IMHO, I recommend first looking at API for something that can do what you are looking for OR ask Angular's Google Group for advice.

这篇关于服务,指令和模块之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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