0

I'm working on an ionic app, I have written an angularjs service for ionic confirm popup,

service

   app.factory("PopupSer", ["$rootScope", "$ionicPopup",
      function ($rootScope, $ionicPopup) {
         return {
            delete: function () {

               $rootScope.popup = $ionicPopup.confirm({
                  title: 'Delete Title', 
                  cssClass: '',
                  subTitle: '',
                  template: '',
                  templateUrl: '',
                  cancelText: 'No',
                  cancelType: '',
                  okText: 'Yes',
                  okType: 'button-balanced'
               });
            }, // delete

            hide: function () {
               $rootScope.popup.hide();
            }

         }; // return
      }
   ]);

Now, I want to change (for example) confirm title, cancelText or okText in my controller, something like this:

controller

PopupSer.delete({
    title: 'Are You Sure About That?'
});

How can I do this when I call service in my controller?

Morteza QorbanAlizade
  • 1,520
  • 2
  • 19
  • 35

2 Answers2

1

You can pass options parameter to the delete function.
This way you can customize the title, cancelText, okText, etc.

ES5

app.factory("PopupSer", ["$rootScope", "$ionicPopup",
    function ($rootScope, $ionicPopup) {
        return {
            delete: function (options) {

                $rootScope.popup = $ionicPopup.confirm({
                    title: options.title || 'Delete Title',
                    cssClass: options.cssClass || '',
                    subTitle: options.subTitle || '',
                    template: options.template || '',
                    templateUrl: options.templateUrl || '',
                    cancelText: options.cancelText || 'No',
                    cancelType: options.cancelType || '',
                    okText: options.okText || 'Yes',
                    okType: options.okType || 'button-balanced'
                });
            }, // delete

            hide: function () {
                $rootScope.popup.hide();
            }

        }; // return
    }
]);

ES6 (use default parameters)

app.factory("PopupSer", ["$rootScope", "$ionicPopup",
    function ($rootScope, $ionicPopup) {
        return {
            delete: function (title = 'Delete Title', cancelText = 'No', okText = 'Yes') {

                $rootScope.popup = $ionicPopup.confirm({
                    title,
                    cssClass: '',
                    subTitle: '',
                    template: '',
                    templateUrl: '',
                    cancelText,
                    cancelType: '',
                    okText,
                    okType: 'button-balanced'
                });
            }, // delete

            hide: function () {
                $rootScope.popup.hide();
            }

        }; // return
    }
]);

Notice that I also use property shorthand notation for title, cancelText, and okText properties in the ES6 code.

attomos
  • 1,112
  • 3
  • 16
  • 30
1

Extend a "default options" object (doc):

app.factory("PopupSer", ["$rootScope", "$ionicPopup",
  function ($rootScope, $ionicPopup) {
     return {
        delete: function (options) {
           var default_options = {
              title: 'Delete Title', 
              cssClass: '',
              subTitle: '',
              template: '',
              templateUrl: '',
              cancelText: 'No',
              cancelType: '',
              okText: 'Yes',
              okType: 'button-balanced'
           };
           $rootScope.popup = $ionicPopup.confirm(angular.extend(default_options, options));
        }, // delete

        hide: function () {
           $rootScope.popup.hide();
        }

     }; // return
  }
]);
Vanojx1
  • 5,574
  • 2
  • 23
  • 37