0

我正在开发一个离子应用程序,我已经为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
      }
   ]);

现在,我想在我的控制器中更改(例如)confirm titlecancelTextokText,如下所示:

控制器

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

当我在控制器中调用服务时,我该怎么做?

4

2 回答 2

1

您可以将options参数传递给删除函数。
这样您就可以自定义title, cancelText,okText等。

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(使用默认参数

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 代码中为、和属性使用属性简写表示法。titlecancelTextokText

于 2016-11-24T07:58:59.473 回答
1

扩展“默认选项”对象(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
  }
]);
于 2016-11-24T08:00:59.477 回答