我已經將功能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