2017-01-02 100 views
1

我已经将功能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在服务未定义(和应用中还有其他插件)。

+0

你用什么库? – Yaser

回答

1

修改window.plugins.toast$cordovaToastshowWithOptions$cordovaToast的一种方法,当然不是window.plugins.toast它显示在git仓库中。我认为这是在有角度的web应用程序中使用,可能不是离子型的。

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); 
    } 
    }; 
+0

不错的尝试,非常感谢。但它不起作用。 'TypeError:无法读取未定义的属性'toast' – NoIdeaHowToFixThis

+0

在我的代码中,没有'toast'。请确保 –

+0

由于未定义的'toast'属性的错误不在我的代码中。可能是你的类型错误。只是检查它 –