【发布时间】:2020-09-08 20:37:18
【问题描述】:
我有一个包含国家和州的 json 文件(作为示例):
{
"countries": [
{
"id": 1,
"name": "United States"
},
{
"id": 2,
"name": "India"
}],
"states": [
{
"id": 1,
"countryId": 1,
"name": "Alabama"
},
{
"id": 2,
"countryId": 1,
"name": "Alaska"
} ] }
现在我有一个服务可以让国家和州稍后显示在我的下拉列表中:
export class CountriesService {
constructor(private http: HttpClient) { }
public getCountries(): Observable<Country[]> {
return this.http.get<Country[]>("assets/data.json").pipe(map(obj => obj["countries"]));
}
public getStates(countryId: number): Observable<State[]> {
return this.http.get<State[]>("assets/data.json").pipe(
map(res => res["states"]),
map(res => { if (res.countryId === countryId) return res;}));
}
}
getCountries() 按预期工作,只返回Countries,但我无法从getStates 方法中获得基于countryId 的特定状态。
它什么也不返回。
我做错了什么?
【问题讨论】:
标签: javascript json angular typescript rxjs