Building on my previous post about creating alert dialogs with AngularJS, here we will look at creating confirmation dialogs we can use to get confirmation from the user before completing a destructive action such as deleting something.
In the directive we add a new scope variable 'confirmPositive' with the '&' binding to allow our controller to assign a function to it. When the OK button is pressed, this function will be called. In the template we add another button - the cancel button will retain the alert behaviour and just hide the dialog whereas the OK button will call a function which hides the dialog and then calls the confirmPositive function.
In the controller we just need to add the function which will carry out our destructive action when the user clicks OK.
In our HTML we just need to add an attribute so that the function in our controller is passed to our directive.
A complete example can be found here.
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.
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.
Subscribe to:
Posts (Atom)