本文介绍了在垫选择中设置默认选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的Angular材质项目中有一个简单的选择选项表单字段:
I have a simple select option form field in my Angular material project:
component.html
component.html
<mat-form-field>
<mat-select [(value)]="modeSelect" placeholder="Mode">
<mat-option value="domain">Domain</mat-option>
<mat-option value="exact">Exact</mat-option>
</mat-select>
</mat-form-field>
[(value)]="modeSelect"
绑定到component.ts文件中的modeSelect属性的地方
Where [(value)]="modeSelect"
is binded to the modeSelect property in the component.ts file
我想做到这一点,以便在页面加载时默认选择<mat-option value="domain">Domain</mat-option>
.
I want to make it so the <mat-option value="domain">Domain</mat-option>
is selected by default on page load.
ng-selected
对我不起作用
推荐答案
无需使用ngModel
或表单
No need to use ngModel
or Forms
在您的html中:
<mat-form-field>
<mat-select [(value)]="selected" placeholder="Mode">
<mat-option value="domain">Domain</mat-option>
<mat-option value="exact">Exact</mat-option>
</mat-select>
</mat-form-field>
,然后在您的组件中,将您的公共属性selected
设置为默认值:
and in your component just set your public property selected
to the default:
selected = 'domain';
这篇关于在垫选择中设置默认选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!