[英]mat-option selected base on if condition
I want to make a mat-option pre checked base on an if
condition. 我想基于
if
条件进行mat-option预先检查。
<div class='form'> <mat-form-field color='accent'> <!-- if UserAccount is not null, 'Yes' should be pre selected --> <mat-select class='form-control' placeholder='user account exists' [(ngModel)]='UserAccountExists' name='UserAccountExists' required value=! data.UserAccount.trim() ? "Yes" : "No"> <mat-option [value]='"Yes"'>Yes </mat-option> <mat-option [value]='"No"'>No </mat-option> </mat-select> <mat-error *ngIf='formControl.invalid'>{{getErrorMessage()}}</mat-error> </mat-form-field> </div> <!-- if "Yes" is selected, this field is 'required' --> <div class='form'> <mat-form-field color='accent'> <input matInput #inputstate class='form-control' placeholder='Username' [(ngModel)]='data.username' name='Username' required> <mat-error *ngIf='formControl.invalid'>{{getErrorMessage()}}</mat-error> </mat-form-field> </div>
The first <div>
: If the UserAccount
exists, "Yes"
should be prechecked. 第一个
<div>
:如果存在UserAccount
,则应预先选中"Yes"
。
If "Yes"
is selected, in the second <div>
, <input>
is required
如果选择
"Yes"
,则在第二个<div>
, required
<input>
Can someone please help? 有人可以帮忙吗?
Thank you 谢谢
<mat-select class='form-control' placeholder='user account exists' [(ngModel)]='UserAccountExists' name='UserAccountExists' required>
<mat-option value="Yes">Yes </mat-option>
<mat-option value="No">No </mat-option>
</mat-select>
...
<input matInput class='form-control' placeholder='Username' [(ngModel)]='data.username' name='Username' [required]="UserAccountExists === 'Yes'">
In your Component, once you fetched your data : 在组件中,一旦您获取了数据:
this.UserAccountExists = !!data.UserAccount ? 'Yes' : 'No';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.