繁体   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