Angular 角度6:错误:找不到不同的支持对象';[对象对象]';类型为';对象';。NgFor只支持绑定到数组之类的可重用文件
我创建了一个angular应用程序,它从本地json文件获取数据。但是我在用html显示数据时遇到了问题。很多变量都是荷兰语,对此我很抱歉,我对所有这些都有点陌生:) 这是我的服务:Angular 角度6:错误:找不到不同的支持对象';[对象对象]';类型为';对象';。NgFor只支持绑定到数组之类的可重用文件,angular,angular6,Angular,Angular6,我创建了一个angular应用程序,它从本地json文件获取数据。但是我在用html显示数据时遇到了问题。很多变量都是荷兰语,对此我很抱歉,我对所有这些都有点陌生:) 这是我的服务: import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse, HttpResponse } from '@angular/common/http'; import { Observable, throwErro
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';
import { City } from './city';
@Injectable({
providedIn: 'root'
})
export class WeatherService {
citiesListUrl = "assets/city.list.json";
constructor(private http: HttpClient) {
}
public getCities(): Observable<HttpResponse<City[]>>{
return this.http.get<City[]>(this.citiesListUrl, {observe: 'response'})
.pipe(retry(3), catchError(this.handleError)
);
}
}
这是HTML代码:
<div class="row">
<div class="col">
<ul>
<li *ngFor="let ci of cities">
{{ci.name}}
</li>
</ul>
</div>
</div>
-
{{ci.name}
我试过另一个答案,他们是有棱角的。我想,他们没有研究我的代码。我也尝试过异步管道,但它可以工作
this.cities = { ... resp.body }},
这应该是
this.cities = [ ... resp.body ]
正如错误所述,*ngFor
将只在数组上迭代,而不是在对象上,所以您需要像我提到的那样将JSON数据推送到数组中
这应该是
this.cities = [ ... resp.body ]
正如错误所述,*ngFor
只会在数组上迭代,而不是在对象上,所以您需要像我提到的那样将JSON数据推送到数组中。[
[
哦,是的!它工作了,非常感谢!我昨天花了一整天的时间和它在一起很高兴:)你可以用upvote和绿色勾选来满足我的答案:PSure,我只是在等待页面允许;-)哦,是的!它工作了,非常感谢!我昨天花了一整天时间和它在一起很高兴:)你可以用upvote来满足我的答案nd绿色勾号:PSure,我只是在等待页面允许;-)
this solved my issue
getBooks()
{
this.isbnsource.getBooks(this.isbn).subscribe(
data => { this.foundBooks = data.json();
this.foundBooks = Array.of(this.foundBooks);
},
err => console.error(err), `enter code here`
() => console.log('getBooks completed')
);
}