我应该叫家长指令控制器或创建服务 [英] Should I call parent directive controller or create service

查看:83
本文介绍了我应该叫家长指令控制器或创建服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说有一个谈话指令,由消息指令。
该消息指令有一个按钮来删除它。
谈话控制器具有从其列表中删除消息的方法。

Let's say there is a "conversation" directive, made of "message" directives. The message directive has a button to delete it. The conversation controller has a method to remove a message from its list.

在角文档,他们说我们可以使用规定从访问父控制器指令的控制器。

In Angular documentation, they say we can use "require" to access a parent controller from a directive controller.

和<一个href=\"http://stackoverflow.com/questions/24740651/require-a-directives-controller-inside-another-directive\">here,答案建议使用需要太多,且仅当您无法使用要求,因为2指令不涉及创建服务。

And here, the answer suggests to use require too, and to create a service only if you can't use require because the 2 directives are not related.

这难道不是一种不好的做法来调用另一个控制器控制器?
我以为是服务通常用于共享控制器之间的信息。

Isn't it a bad practice to call a controller from another controller ? I thought service was typically used to share information between controllers.

难道你不认为这将是更好地创建具有DeleteMessage可以方法的服务,并在消息控制器注入这项服务?

Don't you think it would be better to create a service with the deleteMessage method, and inject this service in the message controller ?

推荐答案

我个人不呼吁家长或root或类似的东西一个功能,它在一种含蓄的方式耦合的粉丝。我preFER使用服务为您提到或注射的具体功能(S)到你的指令。

I'm personally not a fan of calling a function on the parent or root or stuff like that, as its coupled in a kind of implicit way. I prefer to use a service as you mentioned or injecting the concrete function(s) into your directive.

下面是两种方法的优点和缺点。请注意,他们是根据我个人的经验(我不是专家)。

Below are some pros and cons for both approaches. Please note they are based on my personal experience (and I'm not an expert).

服务

服务是单身,这意味着只有一个在你的整个应用程序实例。这可能有几个好处,如果你需要他们:

Services are singletons, meaning there is only one instance in your whole application. This can have several advantages, if you need them:


  • 它总是在那里(你并不需要显式地创建它)

  • 它可以缓存可以是有用的数据,例如保持视图/页
  • 的状态
  • 它可以被用于交换不同组件之间的数据

  • 它可以很容易地被注入在任何你需要它

这也可能导致一些缺点,但:

This can also lead to some disadvantages though:


  • 如果不同的组件共享相同的服务,他们可能会搞乱对方的数据

  • 部件(即指令)将依赖于该服务,使指令的用户他要如何使用该指令没有弹性(例如,当用户点击一个按钮,会发生什么)。这意味着基本上具体功能是硬codeD中的指令。

我个人喜欢用的服务,如果他们有一个直接链接到该指令(并没有别的)或不存储任何状态,例如,如果它们只包含简单的辅助功能。

I personally like to use services, if they have a direct link to the directive (and to nothing else) or do not store any state, for example if they only contain simple helper functions.

如果您的应用程序必须的有一个的会话列表,那么我会考虑实施在暴露像功能的服务方法addMessage DeleteMessage可以 sortBy 等。然后,它可以在到需要访问(中央)列表中的任何成分注入。

If in your application you have one conversation list, then I would consider implementing that in a service that exposes functions like addMessage, deleteMessage, sortBy, etc.. Then it can be injected in to any component which needs access to that (central) list.

传递函数作为参数

在另一方面,如果你有一个的消息 -directive,您可能希望将其用于其它类型的消息以及..?这是不是从会话信息,但也许从邮箱(只是一个愚蠢的例子))。然后,我不会对夫妇的服务和指导。你可以注入 DeleteMessage可以功能全,这在一种情况下会从删除通话,并在其他情况下删除它从邮箱

On the other hand, if you have a message-directive, you might want to use it for other kind of messages as well..? That is messages which are not from the conversation but maybe from a mailbox (just a stupid example ;)). Then I wouldn't couple the service and the directive. You could inject the deleteMessage-function, which in one case would remove it from the conversation and in the other case remove it from the mailbox.

我觉得这两种方法都是有效的,但总是依赖于场景,在你的应用程序的组件是如何共享/重用。

I think both approaches are valid, but always depend on the scenario and how the components are shared/reused over your application.

的元器件上几个层次传递参数(如儿童,儿童儿童指令)第二种方法可能是从一个耦合-perspective更好,但可能会在很多参数的情况下,复杂的和。

The second approach might be nicer from a "coupling"-perspective, but might get complicated in case of lots of parameters and when passing parameters over several levels of components (e.g. to child-child-child directives).

第一种方法是易于implment并且可以具有若干优点,但在组件结果被直接地耦合到该服务,降低其可重用性。

The first approach is easy to implment and can have several advantages, but results in the component being directly coupled to that service, lowering its reusability.

这篇关于我应该叫家长指令控制器或创建服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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