Search code examples
javascriptangularjsconfirm

Providing a template for $confirm dialogs


I'm using angular-confirm to display confirmation messages in a lot of places in my app. For example:

$confirm({
    text: 'content',
    title: 'title text',
    ok: 'Yes',
    cancel: 'No'})
.then(function() {
    doSomething();
});

I want to globally change the layout in which these dialogs appear in the app. I know that angular-confirm allows you to make a global change like this:

$confirmModalDefaults.templateUrl = 'path/to/your/template';

However, this also overrides the template for all $modal.open() calls, which is not what I want.

I think that the way to do this is by a using a provider to append a template url to every $confirm call in the app but I'm not sure exactly how to do that.

How do I create a provider for $confirm and append a templateUrl parameter to every call?


Solution

  • Figured it out:

    function config($provide) {
        $provide.decorator('$confirm', confirmProvider);
    }
    
    /* @ngInject */
    function confirmProvider($delegate) {
        return function (data, settings) {
            if (settings && !settings.template && !settings.templateUrl) {
                settings.template = '<div>my confirm content</div>';
        } else {
            settings = {
                template: '<div>my confirm content</div>'
            }
        }
    
        return $delegate(data, settings);
    };
    

    So now, when $confirm() gets called with data and/or settings, it will apply my custom template unless it's been specified by the caller