簡體   English   中英

Angular ng用於不顯示異步數據

[英]Angular ngFor not displaying async data

為什么我不能在select元素中顯示 this.countries$ 列表?

組件.ts

  countries$! : Observable<String[]>;

  constructor(private statisticService : StatisticService, private fb : FormBuilder) {

    this.getAllCountries();

  }

  getAllCountries() : void {

    this.statisticService.getAllCountries().subscribe((countries) => {
      this.countries$ = of(countries);
      console.log(countries)
    });

  }

服務中的getAllCountries方法返回一個字符串數組的 Observable:

  getAllCountries(): Observable<String[]> {
    return this.http.get<String[]>('http://localhost:8080/countries');
}

這就是我在 HTML 中呈現數據的方式:

<select>
    <option *ngFor="let cntry of countries$ | async" [value]="cntry">
        {{ cntry }}
    </option>
</select>

當我在 subscribe 方法之后使用 console.log(this.countries$) 時,會正確檢索所有國家/地區,但它們不會按應有的方式顯示在 select 元素中。 select 元素正確地顯示了 233 個選項(國家數量),但所有選項都是空白的。

我在這里錯過了什么? 謝謝!

in ts file
export interface Person {
  name: string;
  place: string;
}

myself$: Observable<Person[]>;

<ul *ngIf="myself$ | async as myselfs">
   <li *ngFor="let myself of myselfs; index as i">{{ myself.name }}</li>
</ul> 

https://ultimatecourses.com/blog/angular-ngfor-async-pipe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM