我想在离子弹出窗口中添加ID,但显然不可能使用框架提供的默认选项。
由于它会使我的测试方式更容易使用ID,我试图自己添加它但没有运气。
基本上,我所拥有的是一个创建所有弹出窗口的服务(在下面的代码中,为了简单起见,我只添加一个):
服务:
angular.module('modalsService', []).service('modals', [
'$ionicPopup', '$q', '$ionicSlideBoxDelegate', function($ionicPopup, $q, $ionicSlideBoxDelegate) {
var modals;
modals = this;
modals.deferred = null;
modals.confirm = function(obj, $scope) {
modals.deferred = $q.defer();
$ionicPopup.show({
title: "Are you sure?",
scope: $scope,
cssClass: 'confirmation-modal ' + obj.modalClass,
template: obj.modalMessage,
buttons: [
{
text: "Not Now",
type: 'button-outline button-positive secondary',
onTap: function() {
if (modals.deferred != null) {
return modals.deferred.resolve(false);
}
}
}, {
text: obj.confirmText,
type: 'button-outline button-positive main',
onTap: function() {
if (modals.deferred != null) {
return modals.deferred.resolve(true);
}
}
}
]
});
return modals.deferred.promise;
};
return modals;
}
]);
到目前为止非常简单,一个简单的服务将弹出窗口作为一个承诺返回,在我的控制器中我称之为:
$scope.evtClick = function() {
var test;
test = {
modalClass: "modal-test",
modalMessage: 'Do you really want to ... ?',
confirmText: 'Test'
};
return hgModals.confirm(test, $scope).then(function(res) {
if (res === true) {
return console.log("Clicked the confirm button");
} else {
return console.log("Clicked the cancel button");
}
});
};
我在函数中传递的第一个参数是一个对象,它具有我需要打开的模态的特定选项。
我也想在该对象中添加ID,但是在创建promise之前我不知道如何将它添加到弹出窗口中,这样当我解析promise时,ID已添加到创建弹出窗口的最终代码中。
像这样(服务):
angular.module('modalsService', []).service('modals', [
'$ionicPopup', '$q', '$ionicSlideBoxDelegate', function($ionicPopup, $q, $ionicSlideBoxDelegate) {
var modals;
modals = this;
modals.deferred = null;
modals.confirm = function(obj, $scope) {
modals.deferred = $q.defer();
var myConfirmModal = $ionicPopup.show({ //this is different from before
title: "Are you sure?",
scope: $scope,
cssClass: 'confirmation-modal ' + obj.modalClass,
template: obj.modalMessage,
buttons: [
{
text: "Not Now",
type: 'button-outline button-positive secondary',
onTap: function() {
if (modals.deferred != null) {
return modals.deferred.resolve(false);
}
}
}, {
text: obj.confirmText,
type: 'button-outline button-positive main',
onTap: function() {
if (modals.deferred != null) {
return modals.deferred.resolve(true);
}
}
}
]
});
myConfirmModal... //add ID here maybe? using obj.ID
return modals.deferred.promise;
};
return modals; // the returning modal has already the ID applied
}
]);