亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數據來自API,但不顯示。Angular 9 ABP 框架

數據來自API,但不顯示。Angular 9 ABP 框架

慕森王 2023-09-28 16:38:47
我想在放置在標頭子組件中的選擇控件中填充數據,但數據來自 API,但不顯示。 ngOnInit() {    this._assingedSiteService.getAssignedSitesForLogInUser().subscribe(      (res) => {        this.sites = res;        console.log(this.sites);      },      (error) => {        console.log(error);      }    );  }<li class="nav-item">  <select class="form-control">    <option *ngFor="let site of sites">      {{site.siteName | json}}    </option>  </select></li>
查看完整描述

1 回答

?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

在渲染頁面之前需要等待接收到的數據。你可以做兩件事:


使用布爾值和ngIf指令:


 loadingData = true;

 ngOnInit() {

     this._assingedSiteService.getAssignedSitesForLogInUser().subscribe((res) => {

         this.sites = res;

         console.log(this.sites);

         this.loadingData = false;

       }, (error) => {

         console.log(error);

       }

     );

 }

模板


   <select class="form-control" *ngIf="!loadingData">

     <option *ngFor="let site of sites">

       {{site.siteName | json}}

     </option>

   </select>

我更喜歡,如果您的訂閱中沒有邏輯,請async在模板中使用管道:


 sites$: Observable<Site>;


 ngOnInit() {

    this.sites$ = this._assingedSiteService.getAssignedSitesForLogInUser();

 }

模板:


   <select class="form-control">

     <option *ngFor="let site of sites$ | async">

       {{site.siteName | json}}

     </option>

   </select>


查看完整回答
反對 回復 2023-09-28
  • 1 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號