AngularUI:为什么模态没有作为指令实现? [英] AngularUI: why modal is not implemented as a directive?

查看:33
本文介绍了AngularUI:为什么模态没有作为指令实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用了 angular-ui,我想实现一个模态窗口.库的大部分组件(http://angular-ui.github.io/bootstrap/) 作为指令实现(如预期).但是,模态不是 - 它是作为服务实现的.这会导致对控制器中视图的强烈依赖(即控制器需要知道视图使用的是模态窗口,但不应该是这种情况).

I am using angular-ui in my project and I wanted to implement a modal window. Most of the components of the library (http://angular-ui.github.io/bootstrap/) are implemented as directives (as expected). However, modal is not - it is implemented as a service. This leads to a strong dependency on the view in the controller (i.e. the controller needs to know that the view uses a modal window, what should not be the case).

我的问题是:为什么模态实现为服务,而不是指令?有什么好处吗?

My question is: why is modal implemented as a service, not directive? Does it give any benefits?

推荐答案

$modal 指令非常灵活,通过它的 API,它支持:

The $modal directive is quite flexible, and by its API, it supports:

  • 在您想要的任何事件上触发,因为控制器控制何时打开.
  • 使用resolve选项将数据从控制器传递到对话框中的控制器
  • 在对话框中使用任何控制器或模板.
  • 通过其 result 承诺,在靠近触发控制器的地方将数据从对话框传回.
  • Being triggered on any event you want, since the controller controls when it opens.
  • Passing data from the controller to the controller in the dialog, using the resolve option
  • Using any controller or template in the dialog.
  • Passing data back from the dialog on close to the triggering controller, by its result promise.

虽然并非不可能将所有这些都包含在指令中,但我认为它只能通过使用大量选项或复杂的对象来实现,无论如何都必须在控制器中构造.

While not impossible for this to all be in a directive, I think it could only realistically be achieved by using a lot of options, or a complicated object, which would have to be constructed in the controller anyway.

这不是指令的另一个原因是指令通常用于页面中特定位置的事物.模式本身的设计并未附加到页面中的任何元素.

Another reason for this not being a directive, is that directives are usually for things in a particular place in a page. The modal by its very design is not attached to any element in the page.

我建议尝试为指令设计一个 API,它提供与 $modal 相同的功能,我怀疑它会揭示使用服务总体上更清晰,并且可能更灵活.

I would suggest trying to design an API for a directive that gives the same functionality as $modal, and I suspect it'll reveal that using a service is, on the whole, clearer, and probably more flexible.

这篇关于AngularUI:为什么模态没有作为指令实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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