I'm new to Angular and I need help creating a resuable service or similar to display a very simple Yes / No dialog.
I use the Kendo UI window component to display a modal dialog, but I find that I duplicate an almost identical code / HTML wherever I need a confirmation dialog:
<div kendo-window="myDialogWindow" k-modal="true" k-visible="false" k-width="250"> <div style="text-align: center; width:100%"> <div style="margin:10px 0 15px 0">{{ dialog.message }}</div> <button class="k-button k-primary" id="yesButton" ng-click="onYes()">Yes</button> <button class="k-button" id="noButton" ng-click="onNo()">No</button> </div> </div>
Then from any click event or the like, I call the method to open and center the dialog:
$scope.showDialog = function(title, message) { $scope.dialog.message = message; $scope.myDialogWindow.title(title); $scope.myDialogWindow.center(); $scope.myDialogWindow.open(); }
I would like to minimize code duplication and create something reusable for it.
At first I realized that the user directive would work well, but then I would need to create a two-way related variable in the selection area and put a clock on it to know when to open the dialog + create a couple of binding methods when the user clicks the button. I will also need to put the directive in the HTML.
I was looking for a solution, but the material I found was too tedious for my modest needs (and I also wanted to use the Kendo UI for a consistent theme).
From what I found, it seems that Angular service might be a better approach (entering a dialog box in the DOM), with a promise to tell me what was clicked. The service should be very easy to use - something like this:
dialogService.display("Some title", "The message").then( function() {
Here is a simple plunker that simply opens the kendo dialog when a button is clicked: http://plnkr.co/edit/ywCc3ZqFAXl3JEAu9XHW?p=preview
Recall: how to create a service that displays a Yes / No dialog with a promise to tell me what was clicked? If possible, please update the plunker to demonstrate the solution.
Of course, I am open to suggestions if there is a better approach.