我正在研究离子应用程序,我为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
}
]);
现在,我想在控制器中更改(例如)确认标题, cancelText 或 okText ,像这样:
控制器
PopupSer.delete({
title: 'Are You Sure About That?'
});
当我在控制器中呼叫服务时,如何执行此操作?
答案 0 :(得分:1)
您可以将options
参数传递给删除功能
这样您就可以自定义title
,cancelText
,okText
等。
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
}
]);
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代码中对title
,cancelText
和okText
属性使用了property shorthand notation。
答案 1 :(得分: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
}
]);