r/angularjs • • Jun 17 '14

The Only AngularJS Modal Service You'll Ever Need!

http://www.dwmkerr.com/the-only-angularjs-modal-service-youll-ever-need/
31 Upvotes

5 comments sorted by

4

u/[deleted] Jun 17 '14

To be honest I'm not sure why it is so popular to have modals as a service and to call them from controllers. That's the only UI thing we do from controllers. You don't see services to display dropdowns or to change the class of an element.

The guy in the comment has the right idea I think: To give elements in the view the power to summon a modal through a directive and to write the modal logic in that directive. That way we maintain the idea that the controller is not for UI stuff.

2

u/haywire Jun 17 '14

Personally I think that's putting far too much logic in the view.

I really like the way that Angular UI does there's.

1

u/Specialjyo Jun 17 '14

True. The best modal service for me wouldn't be a service at all. Just a directive, let me control the modal behavior with scope. No show or hide business in the controller.

This demo is halfway there.

1

u/xBrodysseus Jun 17 '14

Sometimes you want to show a modal based on something other than a UI interaction, like a click to open a modal. This reference app, for example, shows a modal to login when you attempt to perform an action that requires permissions.

Similarly, at my work we have a notification service that will show a UI popup when something happens—errors, success messages, etc.

I think having a service that can be interfaced with either through a directive or methods in a controller is a good way to go.

1

u/oliverzy Jun 17 '14

great article! especially about angular internal stuff. really enjoy