Skip to content

Commit dc2b24e

Browse files
committed
feat(popup): Support for programatically closing popup. Fixes #854
1 parent 7510777 commit dc2b24e

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

js/ext/angular/src/service/ionicPopup.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ angular.module('ionic.service.popup', ['ionic.service.templateLoad'])
5353
console.log('Tapped!', res);
5454
}, function(err) {
5555
console.log('Err:', err);
56-
}, function(msg) {
57-
console.log('message:', msg);
56+
}, function(popup) {
57+
// If you need to access the popup directly, do it in the notify method
58+
// This is also where you can programatically close the popup:
59+
// popup.close();
5860
});
5961
6062
// A confirm dialog
@@ -252,7 +254,10 @@ angular.module('ionic.service.popup', ['ionic.service.templateLoad'])
252254
var constructPopupOnScope = function(element, scope) {
253255
var popup = {
254256
el: element[0],
255-
scope: scope
257+
scope: scope,
258+
close: function() {
259+
popAndRemove(this);
260+
}
256261
};
257262

258263
scope.popup = popup;
@@ -318,13 +323,20 @@ angular.module('ionic.service.popup', ['ionic.service.templateLoad'])
318323
};
319324

320325

326+
321327
// Public API
322328
return {
329+
/**
330+
* @private
331+
*/
323332
showPopup: function(data) {
324333
var q = $q.defer();
325334

326335
createPopup(data, q).then(function(popup, scope) {
327336

337+
// Send the popup back
338+
q.notify(popup);
339+
328340
// We constructed the popup, push it on the stack and show it
329341
pushAndShow(popup, data);
330342

@@ -340,7 +352,8 @@ angular.module('ionic.service.popup', ['ionic.service.templateLoad'])
340352
* @name $ionicPopup#show
341353
* @description show a complex popup. This is the master show function for all popups
342354
* @param {data} object The options for showing a popup, of the form:
343-
*
355+
* @returns {Promise} an Angular promise which resolves when the user enters the correct data, and also
356+
* sends the constructed popup in the notify function (for programatic closing, as shown in the example above).
344357
* ```
345358
* {
346359
* content: '', // String. The content of the popup

js/ext/angular/test/popup.html

+19-10
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111

1212
</head>
1313
<body ng-controller="PopupCtrl">
14-
<button class="button button-dark" ng-click="showPopup()">Generic</button>
15-
<button class="button button-primary" ng-click="showConfirm()">Confirm</button>
16-
<button class="button button-balanced" ng-click="showPrompt()">Prompt</button>
17-
<button class="button button-balanced" ng-click="showPasswordPrompt()">Password Prompt</button>
18-
<button class="button button-positive" ng-click="showAlert()">Alert</button>
14+
<ion-content>
15+
<button class="button button-dark" ng-click="showPopup()">Generic</button>
16+
<button class="button button-primary" ng-click="showConfirm()">Confirm</button>
17+
<button class="button button-balanced" ng-click="showPrompt()">Prompt</button>
18+
<button class="button button-balanced" ng-click="showPasswordPrompt()">Password Prompt</button>
19+
<button class="button button-positive" ng-click="showAlert()">Alert</button>
20+
<div style="height: 3000px; width: 50px; background-color: rgba(0,0,0,0.5);"></div>
21+
</ion-content>
1922

2023
<script id="popup-template.html" type="text/ng-template">
2124
<input ng-model="data.wifi" type="text" placeholder="Password">
@@ -94,11 +97,17 @@
9497
};
9598
$scope.showAlert = function() {
9699
$ionicPopup.alert({
97-
title: 'Don\'t eat that!',
98-
content: 'It might taste good'
99-
}).then(function(res) {
100-
console.log('Thank you for not eating my delicious ice cream cone');
101-
});
100+
title: 'Draft Saved',
101+
content: 'Your Data has been saved!'
102+
}).then(function(res) {
103+
console.log('Your Data has been saved!');
104+
}, function(err) {},
105+
function(popup) {
106+
console.log('Got popup', popup);
107+
$timeout(function() {
108+
popup.close();
109+
}, 1000);
110+
});
102111
};
103112
});
104113
</script>

0 commit comments

Comments
 (0)