angular - 如何将选定的下拉值传递给组件
问题描述
我是 Angular 的新手。
我正在关注这个公认的答案。 使用Angular 4根据第一个下拉列表选择第二个下拉列表
我根据下拉菜单创建了下拉菜单,如下所示 -
<select [(ngModel)]="country">
<option *ngFor="let country of countries" [value]="country"> {{ country }} </option>
</select>
<select *ngIf="country" [(ngModel)]="city" [value]="city">
<option *ngFor="let city of cities" [value]="city"> {{ city }} </option>
</select>
export class AppComponent {
private map = new Map<string, string[]>([
['Poland', ['Warszawa', 'Krakow']],
['USA', ['New York', 'Austin']],
])
country: string;
city: string;
get countries(): string[] {
return Array.from(this.map.keys());
}
get cities(): string[] | undefined {
return this.map.get(this.country);
}
}
现在我想——
将选定值传递给组件。
我尝试像这样传递静态值 -
<app-render country="USA" city="Austin"></app-render>
这很好用
所以,为了传递选定的值,我尝试这样 -
<app-render [country]="country" [city]="city"></app-render>
请帮助/指导。
解决方案
<select [(ngModel)]="input-country">
<option *ngFor="let country of countries" [value]="country"> {{ country }}</option>
</select>
<select *ngIf="country" [(ngModel)]="input-city" >
<option *ngFor="let city of cities" [value]="city"> {{ city }} </option>
</select>
接着
<app-render [country]="input-country" [city]="input-city"></app-render>
推荐阅读
- python - 如果部分来自 elif 部分,我如何重新触发?
- c - 带有 shell 输出重定向的 execvp shell 命令不起作用
- java - Java:Google PubSub 服务器在确认截止日期(10 秒)后不重新发送消息
- asp.net-core-2.0 - 使用 Net Core 2.2 覆盖 appsettings.production 中的 Serilog 设置
- python - 根据元素长度删除python数据框中字符串的元素
- reactjs - 在 TypeScript 中将 RefObject 反应为数组
- r - 格式化数据框以允许进行闪避的条形图
- swift - IOS 14 UNNotificationPresentationOptions.list,.banner 与警报
- python - 如何在我创建的每个新文件夹中包含一个“存档”文件夹?
- javascript - 在属性中对象 this 关键字