在Angular 6中迭代dropdown函数的多嵌套JSON对象,可以通过以下步骤实现:
jsonData = {
"countries": [
{
"name": "China",
"cities": [
{
"name": "Beijing",
"areas": [
"Dongcheng",
"Xicheng",
"Chaoyang"
]
},
{
"name": "Shanghai",
"areas": [
"Pudong",
"Huangpu",
"Xuhui"
]
}
]
},
{
"name": "USA",
"cities": [
{
"name": "New York",
"areas": [
"Manhattan",
"Brooklyn",
"Queens"
]
},
{
"name": "California",
"areas": [
"Los Angeles",
"San Francisco",
"San Diego"
]
}
]
}
]
};
<select>
<option *ngFor="let country of jsonData.countries" [value]="country.name">{{ country.name }}</option>
</select>
<select>
<option *ngFor="let city of jsonData.countries[selectedIndex]?.cities" [value]="city.name">{{ city.name }}</option>
</select>
<select>
<option *ngFor="let area of jsonData.countries[selectedIndex]?.cities[selectedIndex2]?.areas" [value]="area">{{ area }}</option>
</select>
selectedIndex: number;
selectedIndex2: number;
onCountryChange(event) {
this.selectedIndex = event.target.selectedIndex;
this.selectedIndex2 = 0; // 重置第二个下拉框的索引
}
<select (change)="onCountryChange($event)">
<option *ngFor="let country of jsonData.countries" [value]="country.name">{{ country.name }}</option>
</select>
通过以上步骤,你可以在Angular 6中迭代dropdown函数的多嵌套JSON对象。当用户选择不同的选项时,第二个和第三个下拉框将根据用户的选择进行更新。请注意,这只是一个简单的示例,你可以根据实际需求进行修改和扩展。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云