8

I am using ngDialog in my application and I would like to create a generic confirm modal , which I can use whenever I need , the confirm message is going to be different .

My questions :

1- Is creating a directive with the ngDialog functionality a good idea and what is its design ?

2- what is the difference between confirm() and openConfirm() in ngDialog code .

Thanks in advance

Divya MV
  • 2,021
  • 3
  • 31
  • 55
sisimh
  • 1,287
  • 3
  • 20
  • 37

1 Answers1

20

Well, to answer your questions,

1 - You can create a directive for it, having a scope, say type to which you pass the confirmation type ( i.e. submit for submit confirmations, delete for delete confirmations) and the directive should render the message based on the type you specified.

2 - openConfirm() is a type of ngDialog, which can only be closed by confirming the action (unlike ngDialog.open()), so you don't have the ability here to close the dialog when clicking anywhere in theDOM. confirm() is just a method you use to close the dialog, you use this method to close the dialog and resolve the promise that was returned when opening the modal, so it can go on <button ng-click="confirm()">Confirm</button> inside your dialog.

Hope this helped you

Update

openConfirm() Opens a dialog that by default does not close when hitting escape or clicking outside the dialog window. The function returns a promise that is either resolved or rejected depending on the way the dialog was closed.

To resolve the promise, your dialog should be like this:

With ngDialog controller

ngDialog.openConfirm({
    template: '<div></div>',
    controller: ['$scope', function($scope) { 
      // Controller logic here
    }]
}).then(function (success) {
    // Success logic here
}, function (error) {
    // Error logic here
});

With directive controller

ngDialog.openConfirm({
    template: '<div></div>',
    scope: $scope, // <- ability to use the scopes from directive controller
}).then(function (success) {
    // Success logic here
}, function (error) {
    // Error logic here
});

You can use your directive controller as long as you pass scope: $scope inside the dialog

Here is a demo showing you how you can use type

Try switching the type in index.html from confirm to remove and see the updated content and button text in the dialog

Community
  • 1
  • 1
Razvan B.
  • 6,721
  • 2
  • 19
  • 32
  • Thanks Razvan for your answer , and how can I resolve th promise after confirm ? how to get it ? and do i need to connect a controller with the ngDialog or I can have my own directive controller ? and if I use openConfirm() with showClose button true , it will mean i am creating same as open() but the only difference is that clicking outside the modal will not close my openConfirm modal ? if so they would just make the click outside to close as boolean functionality . if you please clarify to me. @razvan – sisimh Jul 28 '15 at 09:05
  • @sisimh I've just updated my answer with explanations – Razvan B. Jul 28 '15 at 09:26
  • Thanks @Razvan Balosin this surely helped me :) , a small question about what you mentioned with type , what would it differ between submit and delete ? im thinking the CSS like button colors , are there some classes in ngdialog css files i can use for the buttons ? – sisimh Jul 28 '15 at 09:38
  • @sisimh I've just added a demo. You can create any sort of variables inside your directive to use it everywhere across your app. Look into the demo to see how the button text and content get's updated based on the type you specify – Razvan B. Jul 28 '15 at 09:59
  • what if I have some custom logic that requests data inside my modal ? Like I have a list of items and upon selecting an option i need to retrieve some data from the backend about it ,,, since im creating a directive i would need to cover this case – sisimh Jul 28 '15 at 21:11
  • You can add that logic anywhere, either the directive controller or the dialog controller itself – Razvan B. Jul 28 '15 at 21:50
  • all I can interact with this directive is through the parameters I send to it . so I was asking if I can cover these cases where the modal body requires http requests on some actions and to cover this in a generic design , this will be used by other developers .any suggestions @Razvan? – sisimh Jul 28 '15 at 22:11