angular - reusable dialogs

后端 未结 3 1750
小鲜肉
小鲜肉 2021-01-02 21:51

I need to build a dialog to be used with any item on a list of items. The dialog is pretty much the same regardless of the item except for the values of the fields which are

3条回答
  •  难免孤独
    2021-01-02 22:32

    This is indeed excellent question and I'm happy to see that more and more people are starting to approach dialogs as services.

    Regarding your particular questions, here are some of my thoughts:

    • You can "cache" linking function (that is - function that is returned from the $compile call) and then call this function as needed (passing in scope variables).
    • Instead of inserting (hidden) compiled element you could only attach it on demand, when a dialog gets opened. On top of this I would rather attach modal element to the $rootElement instead of just not to touch DOM elements above where ng-app was defined. Just not to touch parts of the DOM that AngularJS is not controlling.
    • IMO dialogs are really close to AngularJS routes (as they provide different "views") and as such it would be very nice to have ability to resolve promises before modal is shown (as with routes).

    In fact there are number of things to consider when designing a good, generic dialog service and I hope that those advice, alongside with excellent input provided by others, will get you started. But this all is a bit theoretical so if you are looking at the implementation of what was discussed here you can have a look at this implementation. ($dialog service from http://angular-ui.github.com/bootstrap/ - it is fully customizable so can be used with CSS other than Bootstrap's. Documentation here).

    It can be seen in action in this plunk: http://plnkr.co/edit/PG0iHG?p=preview

提交回复
热议问题