2017-09-25 22 views
1

在我的情況下,我有一個離子頁面顯示不同模板中的選定項目。對於每個項目類型我有設置對象,可以使用ngOnint從服務器獲取選擇項目後,我渲染HTML取決於這些設置。例如我使用* ngIf顯示/隱藏某個組件。如何在Ionic 2/3中呈現html頁面之前從url promise加載數據?

問題出在html文件中,設置對象是未定義的。
如何在呈現html之前獲取設置對象。 說明:我正在使用servlet的web服務。

 <div class="user-details"> 
     <ion-segment class="user-content-segment" [(ngModel)]="display"> 
      <ion-segment-button value="description"> 
       Açıklama 
      </ion-segment-button> 

      <ion-segment-button *ngIf="settings.Commmon.UseMenuList" value="prices"> 
       Fiyatlar 
      </ion-segment-button> 
      <ion-segment-button value="location"> 
       Konum 
      </ion-segment-button> 
      <ion-segment-button value="reviews"> 
       Yorumlar 
      </ion-segment-button> 
     </ion-segment> 
    </div> 

TS文件

constructor(public navCtrl: NavController, 
      public navParams: NavParams, private postApi: PostAPI, 
      public loadingCtrl: LoadingController, public alerCtrl: AlertController) { 
    this.loading = this.loadingCtrl.create(); 
    this.post = navParams.get('post'); 
} 

ngOnInit() { 
    this.postApi.GetPostTypeSetting(this.post.PostTypeId).then(res => { 
     this.settings = res; 
     console.log(res); 
    }); 
    this.postApi.GetPostDetail(this.post.PostId).then(res => { 
     this.postDetail = res; 
     console.log(res); 
    }); 

    this.postApi.GetCategoryDetail(1, 1).then(res2 => { 

    }); 

} 

回答

1

您需要定義您的變量的標準值。 *ngIf將不斷檢查條件是否已更改。所以,你定義是這樣的:

public settings: object = { 
    Common: { 
     UseMenuList: false 
    } 
}; 

在你得到一個錯誤的時刻,因爲你試圖訪問未定義的變量多數民衆贊成,因爲你的API請求是異步的。所以,只需定義一個默認值(無論是true還是false),你就會好起來的。

0

的離子可以使用ionViewWillEnter()功能用於此目的

+0

問題仍然相同,它說設置對象是未定義的。我已添加此代碼以將輸入方法ionViewWillEnter()this.nBar.setBackButtonText(「」); (res。> this.postings = res; console.log(res); }); } –

0

也許你在找什麼是ionViewCanEnter()

奔跑在視圖可以進入之前。這可以被用來作爲一種在您需要檢查的權限認證的意見「後衛」的前視圖可以進入

https://ionicframework.com/docs/api/navigation/NavController/

ionViewCanEnter(): boolean { 

    this.postApi.GetPostTypeSetting(this.post.PostTypeId).then(res => { 
     this.settings = res; 
     console.log(res); 

     this.postApi.GetPostDetail(this.post.PostId).then(res2 => { 
      this.postDetail = res2; 
      console.log(res2); 

      this.postApi.GetCategoryDetail(1, 1).then(res3 => { 
       console.log(res3); 
       return true; 
      }); 
     }); 
    }); 
} 
相關問題