【发布时间】:2018-04-03 22:58:29
【问题描述】:
我有一个这样的 JSON 文件,总结一下:
{
"id": "1",
"country": "Brazil",
"state": [
{"id": "1", "name": "Acre",
"city": [ { "id": "1", "name": "Rio Branco"},
{ "id": "2", "name": "Xapuri"},
{ "id": "3", "name": "Cruzeiro do Sul"} ]
}
我在视图中创建了 3 个选择选项,我必须首先选择国家,然后根据国家 ID,我需要用州填充第二个选择选项。选择州后,我需要用城市填写第三个选择选项。
我创建了返回所有 JSON 文件的 PlacesService:
getPlaces() {
return this.http.get('assets/database/places.json')
.map( (res: Response) => res.json());
}
然后在我的组件中我调用这个服务并且运行良好:
this.placesService.getPlaces().subscribe(dados => this.places= dados);
好的,我知道如何返回 JSON 文件的所有数据,但我不知道如何在 JSON 文件中搜索特定 id 并仅返回与这些 ID 相关的对象。
我想知道如何解决这个问题以及最佳实践是什么,使用唯一 json 文件中的所有对象或划分其他 json 文件(例如:countries.json、states.json、citys.json)
【问题讨论】:
标签: angular typescript angular5 angular-http