angular - Angular - 如何在下拉菜单中选择值?
问题描述
目标:我希望能够让用户单击下拉列表中的值,这将根据选择更改他们所在站点的地址。
问题:我能够让它更改地址,但无论他们做出什么选择,它都会这样做。我希望它为每个选择转到不同的地址。
因此,如果他们选择“英语”,我希望它转到 site.com/en。如果他们选择西班牙语,我希望它转到 site.com/es
我尝试了什么:
我尝试了该解决方案,以及它的许多变体:How to get Id of selected value in Mat-Select Option in Angular 5 我还查看了 Angular 材料文档,但对于本主题所需的所有内容都有些稀疏。
为什么不识别特定的选择?
HTML:
<mat-form-field class="right">
<mat-select>
<mat-option *ngFor="let language of languages" [value]="language.value" (selectionChange)="doSomething($event.value)">
{{language.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
打字稿:
doSomething(event) {
//if value selected is spanish
if(event.value == "es")
this.routerService.navigate(['es/']);
}
解决方案
尝试将您的 selectionChanged 移动到您的选择,而不是选项:
<mat-form-field class="right">
<mat-select (selectionChange)="doSomething($event)">
<mat-option *ngFor="let language of languages" [value]="language.value" >
{{language.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
推荐阅读
- r - 如何在 R 中获取列表的相同标签
- python-3.x - 如何在 Python 的文本中查找哪些字符串(在一大串字符串中)?
- c# - 访问 SQL Server 2012 Express 时出现登录错误
- java - 用户输入确定Java中数组的长度
- ruby-on-rails - 关闭在 rails 中记录 base64 编码的附件
- python - Python,Nameko,nameko-sqlalchemy:错误:db_uris = self.container.config[DB_URIS_KEY] KeyError:'DB_URIS'
- javascript - 从字符串解释中提取数字
- python - Boto3 AWS 创建用户 Try catch 不允许 for 循环继续
- arrays - 为什么 Knex 不能正确识别 Postgres 数组列?
- javascript - Bootstrap-4 - 尝试在行上进行平滑渐变悬停