Saturday, 25 June 2016

AngularJS Dialogs - Alert Dialogs

Moving from working with just jQuery to AngularJS can take some getting used to. Separating the front end from the logic of the controller throws up some interesting problems such as displaying alert and confirmation dialogs.

Here's one way to trigger a custom alert dialog from a controller.

To achieve this we create an AngularJS directive which, when activated, will cover the screen with a full-screen translucent element with a child element which will be our pop-up dialog.


We have a simple template with the full-screen background and a dialog with title, content and an OK button. When the button is clicked, visible is set to false. A link function initialises the visible variable to false and we set up some variables so that the title and content can be set and a variable mapped to the visible variable.

In our controller we can create a function to show our alert. In this example, when a button is clicked, we will set our alert message and show the alert:


The HTML including our alert directive looks like this:


A complete example can be found here.

No comments:

Post a Comment