2017-01-02 101 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'屬性的錯誤不在我的代碼中。可能是你的類型錯誤。只是檢查它 –