Skip to content

Commit 046ad53

Browse files
committed
feat(cordovaEvents): $ionicPlatform.on method
Create $ionicPlatform.on(type, callback) to make it easier to add Cordova event listeners. Closes #2219
1 parent 37d75f7 commit 046ad53

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: js/angular/service/platform.js

+22
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,28 @@ IonicModule
177177
return ionic.Platform.is(type);
178178
},
179179

180+
/**
181+
* @ngdoc method
182+
* @name $ionicPlatform#on
183+
* @description
184+
* Add Cordova event listeners, such as `pause`, `resume`, `volumedownbutton`, `batterylow`,
185+
* `offline`, etc. More information about available event types can be found in
186+
* [Cordova's event documentation](https://cordova.apache.org/docs/en/edge/cordova_events_events.md.html#Events).
187+
* @param {string} type Cordova [event type](https://cordova.apache.org/docs/en/edge/cordova_events_events.md.html#Events).
188+
* @param {function} callback Called when the Cordova event is fired.
189+
* @returns {function} Returns a deregistration function to remove the event listener.
190+
*/
191+
on: function(type, cb) {
192+
ionic.Platform.ready(function(){
193+
document.addEventListener(type, cb, false);
194+
});
195+
return function() {
196+
ionic.Platform.ready(function(){
197+
document.removeEventListener(type, cb);
198+
});
199+
};
200+
},
201+
180202
/**
181203
* @ngdoc method
182204
* @name $ionicPlatform#ready

0 commit comments

Comments
 (0)