【发布时间】:2021-06-10 08:39:31
【问题描述】:
我正在使用版本 10 中的 Angular 应用程序。低于使用硬编码 json 对象学习 observables 而没有 http 调用的要求。例如我有一个接口
export interface cities {
cityId: number;
cityName: string;
}
下面是我声明一个 observable 的代码
cities$: Observable<city[]>;
在 Oninit 方法中,我创建了这样的对象
ngOnInit() : void {
const cityData = [
{
cityId: 1,
cityName: 'Bangalore'
},
{
cityId: 2,
cityName: 'chennai'
}
];
// here i want to get the values in the observables. how can i do it
this.cities$ = ...
}
下面是html代码
<ul>
<li *ngFor='let city of cities$ | async'>
<span [innerHTML] = 'city.cityName'>
</li>
</ul>
【问题讨论】: