如何在 angularjs 中更改控制器中的服务选项

how to change service options in controller in angularjs

我正在开发一个离子应用程序,我已经为 ionic confirm popup

编写了一个 angularjs 服务

服务

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

现在,我想更改(例如)确认标题取消文本okText 在我的 控制器 中,是这样的:

控制器

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

当我在我的控制器中调用服务时,我该如何做到这一点?

您可以将 options 参数传递给删除函数。
这样你就可以自定义titlecancelTextokText

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(使用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
    }
]);

请注意,我还在 ES6 代码中将 property shorthand notation 用于 titlecancelTextokText 属性。

扩展 "default options" 对象 (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
  }
]);