2016-12-09 41 views

回答

1

請看看這段代碼;我使用它的不同的東西,但我想它可以派上用場:

import { Nav, Platform, IonicApp, ... } from 'ionic-angular'; 

@Component({ 
    selector: 'page-custom', 
    templateUrl: 'custom.html' 
}) 
export class CustomPage { 

    constructor(private platform: Platform, 
       private ionicApp: IonicApp) { 

     // ... 
    } 

    public showModalByClosingPreviousOne(): void { 

     let activePortal = this.ionicApp._loadingPortal.getActive() || 
      this.ionicApp._modalPortal.getActive() || 
      this.ionicApp._toastPortal.getActive() || 
      this.ionicApp._overlayPortal.getActive(); 

     if (activePortal) { 

      // Dismiss the active portal 
      activePortal.dismiss(); 
      activePortal.onDidDismiss(() => { 

       // Here you can show the new one... 

      }); 
      return; 
     } 
    } 

    // Or maybe you can just use the `activePortal` property to avoid 
    // showing another loading instead of closing the previous one. 
    public showNewModal(): void { 

     let activePortal = this.ionicApp._loadingPortal.getActive() || 
      this.ionicApp._modalPortal.getActive() || 
      this.ionicApp._toastPortal.getActive() || 
      this.ionicApp._overlayPortal.getActive(); 

     if (!activePortal) { 

      // Show your modal 
     } 
    } 

} 
+1

這是件好事。但我需要檢測它不在組件中。我需要在我的一個服務課程中檢測它。你有任何解決方案? –

+0

您的服務可以注入ionicapp類,並在那裏檢查。 – misha130

+0

@AlexanderZakusilo從服務中執行與視圖相關的事情(如顯示加載)是非常糟糕的做法。在調用服務之前,您應該檢查是否需要在組件中顯示模式。服務完成後,再次從組件中隱藏加載窗口。 – sebaferreras