Ionic彈窗服務(wù)允許程序創(chuàng)建、顯示彈出窗口,需要用戶繼續(xù)響應(yīng)。
彈窗系統(tǒng)支持更多靈活的構(gòu)建alert(),prompt()和confirm()功能版本,以及用戶習(xí)慣,除了允許查看完全自定義內(nèi)容的的彈窗。
用法
一些基本的例子,查看下文了解所有可用的選項(xiàng)詳情。
angular.module('mySuperApp', ['ionic'])
.controller(function($scope, $ionicPopup, $timeout) {
// 觸發(fā)一個(gè)按鈕點(diǎn)擊,或一些其他目標(biāo)
$scope.showPopup = function() {
$scope.data = {}
// 一個(gè)精心制作的自定義彈窗
var myPopup = $ionicPopup.show({
template: '<input type="password" ng-model="data.wifi">',
title: 'Enter Wi-Fi Password',
subTitle: 'Please use normal things',
scope: $scope,
buttons: [
{ text: 'Cancel' },
{
text: '<b>Save</b>',
type: 'button-positive',
onTap: function(e) {
if (!$scope.data.wifi) {
//不允許用戶關(guān)閉,除非他鍵入wifi密碼
e.preventDefault();
} else {
return $scope.data.wifi;
}
}
},
]
});
myPopup.then(function(res) {
console.log('Tapped!', res);
});
$timeout(function() {
myPopup.close(); //由于某種原因3秒后關(guān)閉彈出
}, 3000);
// 一個(gè)確認(rèn)對話框
$scope.showConfirm = function() {
var confirmPopup = $ionicPopup.confirm({
title: 'Consume Ice Cream',
template: 'Are you sure you want to eat this ice cream?'
});
confirmPopup.then(function(res) {
if(res) {
console.log('You are sure');
} else {
console.log('You are not sure');
}
});
};
// 一個(gè)提示對話框
$scope.showAlert = function() {
var alertPopup = $ionicPopup.alert({
title: 'Don\'t eat that!',
template: 'It might taste good'
});
alertPopup.then(function(res) {
console.log('Thank you for not eating my delicious ice cream cone');
});
};
};
});
方法
show(可選)
對話框。 這是一個(gè)所有彈窗的主體顯示功能。
一個(gè)帶有按鈕組的復(fù)雜彈窗,每個(gè)按鈕帶有一個(gè)文本 和 類型字段,此外還有一個(gè) onTap
功能。當(dāng)點(diǎn)擊彈窗上的相關(guān)按鈕,會觸發(fā)onTap
函數(shù),默認(rèn)會關(guān)閉彈窗,處理彈窗的返回值。如果你想阻止默認(rèn)動作,點(diǎn)擊按鈕保持打開彈窗,當(dāng)點(diǎn)擊一個(gè)事件時(shí),觸發(fā)event.preventDefault()
。詳見下文。
{
title: '', // String. 彈窗的標(biāo)題。
subTitle: '', // String (可選)。彈窗的子標(biāo)題。
template: '', // String (可選)。放在彈窗body內(nèi)的html模板。
templateUrl: '', // String (可選)。在彈窗body內(nèi)的html模板的URL。
scope: null, // Scope (可選)。一個(gè)鏈接到彈窗內(nèi)容的scope(作用域)。
buttons: [{ //Array[Object] (可選)。放在彈窗footer內(nèi)的按鈕。
text: 'Cancel',
type: 'button-default',
onTap: function(e) {
// 當(dāng)點(diǎn)擊時(shí),e.preventDefault() 會阻止彈窗關(guān)閉。
e.preventDefault();
}
}, {
text: 'OK',
type: 'button-positive',
onTap: function(e) {
// 返回的值會導(dǎo)致處理給定的值。
return scope.data.response;
}
}]
}
返回: object 當(dāng)關(guān)閉彈窗時(shí),處理一個(gè)promise。有一個(gè)附加的關(guān)閉函數(shù),用于利用程序關(guān)閉彈窗。
alert(可選)
顯示一個(gè)帶有一段信息和一個(gè)用戶可以點(diǎn)擊關(guān)閉彈窗的按鈕的簡單提示彈窗。
{
title: '', // String. 彈窗的標(biāo)題。
subTitle: '', // String (可選)。彈窗的子標(biāo)題。
template: '', // String (可選)。放在彈窗body內(nèi)的html模板。
templateUrl: '', // String (可選)。 放在彈窗body內(nèi)的html模板的URL。
okText: '', // String (默認(rèn): 'OK')。OK按鈕的文字。
okType: '', // String (默認(rèn): 'button-positive')。OK按鈕的類型。
}
返回: object 當(dāng)彈窗關(guān)閉時(shí),處理的一個(gè) promise。有一個(gè)額外的關(guān)閉函數(shù),可以被帶有任何給定的值的關(guān)閉程序調(diào)用。
confirm(可選)
顯示一個(gè)簡單的帶有一個(gè)取消和OK按鈕的對話框彈窗。
如果用戶點(diǎn)擊OK按鈕,就設(shè)置promise為true,如果用戶點(diǎn)擊取消按鈕則為false。
{
title: '', // String. 彈窗標(biāo)題。
subTitle: '', // String (可選)。彈窗的副標(biāo)題。
template: '', // String (可選)。放在彈窗body內(nèi)的html模板。
templateUrl: '', // String (可選)。放在彈窗body內(nèi)的一個(gè)html模板的URL。
cancelText: '', // String (默認(rèn): 'Cancel')。一個(gè)取消按鈕的文字。
cancelType: '', // String (默認(rèn): 'button-default')。取消按鈕的類型。
okText: '', // String (默認(rèn): 'OK')。OK按鈕的文字。
okType: '', // String (默認(rèn): 'button-positive')。OK按鈕的類型。
}
返回: object 當(dāng)關(guān)閉對話框時(shí),處理的一個(gè)promise。當(dāng)彈窗關(guān)閉時(shí),處理的一個(gè) promise。有一個(gè)額外的關(guān)閉函數(shù),可以被帶有任何給定的值的關(guān)閉程序調(diào)用。
prompt(可選)
顯示一個(gè)簡單的提示彈窗,帶有一個(gè)input, OK 按鈕,和取消按鈕。如果用戶點(diǎn)擊OK,就設(shè)置promise的值,如果用戶點(diǎn)擊取消,則值為未定義。
$ionicPopup.prompt({
title: 'Password Check',
template: 'Enter your secret password',
inputType: 'password',
inputPlaceholder: 'Your password'
}).then(function(res) {
console.log('Your password is', res);
});
{
title: '', // String. 彈窗的標(biāo)題。
subTitle: '', // String (可選)。彈窗的副標(biāo)題。
template: '', // String (可選)。放在彈窗body內(nèi)的html模板。
templateUrl: '', // String (可選)。放在彈窗body內(nèi)的html模板的URL。
inputType: // String (默認(rèn): 'text')。input的類型。
inputPlaceholder: // String (默認(rèn): '')。input的 placeholder。
cancelText: // String (默認(rèn): 'Cancel')。取消按鈕的文字。
cancelType: // String (默認(rèn): 'button-default')。取消按鈕的類型。
okText: // String (默認(rèn): 'OK')。OK按鈕的文字。
okType: // String (默認(rèn): 'button-positive')。OK按鈕的類型。
}
返回: object 當(dāng)關(guān)閉對話框時(shí),處理的一個(gè)promise。當(dāng)彈窗關(guān)閉時(shí),處理的一個(gè) promise。有一個(gè)額外的關(guān)閉函數(shù),可以被帶有任何給定的值的關(guān)閉程序調(diào)用。