Morteza QorbanAlizade Morteza QorbanAlizade - 8 days ago 6
AngularJS Question

how to change service options in controller in angularjs

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?

Answer

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
  }
]);
Comments