我已经将功能showToast从控制器移至服务。 目标是通过不同的控制器和指令共享showToast代码。window.plugins在服务中未定义
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);
}
};
});
重构之后,我得到如下:
TypeError: Cannot read property 'toast' of undefined at Object.showToast (ToastService.js:7) at ReportProblemModalController.js:77
不知道如何在服务中显示window.plugins
。
编辑
不知道这是否有助于:
"cordova-plugin-x-toast"
{
"variables": {
"FABRIC_API_KEY": "xxx",
"FABRIC_API_SECRET": "yyy"
},
"locator": "cordova-fabric-plugin",
"id": "cordova-fabric-plugin"
}
],
...但是,我有感觉的概率是其他地方,如window.plugin
在服务未定义(和应用中还有其他插件)。
你用什么库? – Yaser