【发布时间】:2018-10-11 15:51:02
【问题描述】:
我想根据上一个选择框中的国家/地区在选择框中显示一个城市名称。我在第一个选择框中显示了国家,但是在点击事件时我无法显示城市,我不知道如何将嵌套的json访问到html中。请帮助。
这是我的 json 文件
[
{
"countryName": "India",
"cities": [
{
"id": 1,
"name": "Banglaore",
"founded": -200,
"beautiful": true,
"data": 123,
"keywords": ["Kaveri", "River"]
},
{
"id": 1,
"name": "Pune",
"founded": 0,
"beautiful": false,
"data": "no",
"keywords": ["FSDF", "FS"]
}
]
},
{
"countryName": "US",
"cities": [
{
"id": 2,
"name": "CA",
"founded": -200,
"beautiful": true,
"data": 123,
"keywords": ["RT", "SSF"]
},
{
"id": 2,
"name": "NY",
"founded": 0,
"beautiful": false,
"data": "no",
"keywords": ["GSG", "DG"]
}
]
}
]
app.component.ts 文件是
import { Component } from '@angular/core';
import { Http,Response } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private http: Http) { }
public myData;
public v;
ngOnInit(){
this.http.get("assets/file.json")
.map((res:Response)=> res.json())
.subscribe(data =>{this.myData = data})
}
onChange(event): void { // event will give you full breif of action
const newVal = event.target.value;
this.v =newVal;
}
}
app.html 文件是
<select (change)="onChange($event)" >
<option value="0">--All--</option>
<option *ngFor="let d of myData">
{{d.countryName}}
</option>
</select>
<br/>
<br/>
<select >
<option value="0">--All--</option>
<option *ngFor="let object of myData" value="object.countryName" [selected]="object.countryName === v">{{object.cities}}</option>
</select>
【问题讨论】:
标签: json angular angular5 angular2-template angular2-directives