I have moved the function showToast from a controller to a service. The goal is to share the showToast code across different controllers and directives.
var services = angular.module("myapp.services");
services.service('toast', function($cordovaToast){
this.showToast = function(msg) {
try {
window.plugins.toast.showWithOptions(
{
message: msg,
duration: "long", // which is 2000 ms. "long" is 4000. Or specify the nr of ms yourself.
position: "bottom",
},
{}, // optional
{} // optional
);
}
catch(e) {
console.log(e);
}
};
});
After the refactoring, I get the following:
TypeError: Cannot read property 'toast' of undefined at Object.showToast (ToastService.js:7) at ReportProblemModalController.js:77
Not sure how to expose window.plugins
in a service.
EDIT
Not sure if this helps:
"cordova-plugin-x-toast"
{
"variables": {
"FABRIC_API_KEY": "xxx",
"FABRIC_API_SECRET": "yyy"
},
"locator": "cordova-fabric-plugin",
"id": "cordova-fabric-plugin"
}
],
... however, I have the feeling the prob is elsewhere, as window.plugin
is undefined in the service (and there are other plugins in the app).
modify window.plugins.toast
to $cordovaToast
. showWithOptions
is a method of $cordovaToast
, not window.plugins.toast
of course it is shown in the git repository . I think that is to be used in angular web app, may not in ionic.
this.showToast = function(msg) {
try {
$cordovaToast.showWithOptions(
{
message: msg,
duration: "long", // which is 2000 ms. "long" is 4000. Or specify the nr of ms yourself.
position: "bottom",
},
{}, // optional
{} // optional
);
}
catch(e) {
console.log(e);
}
};