Skip to content

Instantly share code, notes, and snippets.

@taly2808
Forked from rajatrocks/Ionic Cordova Toast
Last active November 25, 2016 08:29
Show Gist options
  • Save taly2808/f6703f6c22d77473616c to your computer and use it in GitHub Desktop.
Save taly2808/f6703f6c22d77473616c to your computer and use it in GitHub Desktop.
// Requires the Toast plugin: https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin
// And Ionic Framework: http://ionicframework.com
// ngCordova is used here, but easily removed: http://ngcordova.com/
// When running in Cordova, show the native toast. Outside of Cordova, show an Ionic Popup for the same period of time.
// Uses the API for the Toast plugin - message, duration, position.
// Differences are that: Ionic Popup ignores position, and doesn't allow doing anything while it shows.
.factory('Toast', function($rootScope, $timeout, $ionicPopup, $cordovaToast) {
return {
show: function (message, duration, position) {
message = message || "There was a problem...";
duration = duration || 'short';
position = position || 'top';
if (!!window.cordova) {
// Use the Cordova Toast plugin
$cordovaToast.show(message, duration, position);
}
else {
if (duration == 'short') {
duration = 2000;
}
else {
duration = 5000;
}
var myPopup = $ionicPopup.show({
template: "<div class='toast'>" + message + "</div>",
scope: $rootScope,
buttons: []
});
$timeout(function() {
myPopup.close();
}, duration);
}
}
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment