如何将 angular mat 单选按钮设置为默认选中?
How to set an angular mat radio button to selected by default?
请问,我使用的是更新angular版本。我试图将垫子单选按钮设置为默认选中,我尝试使用 [checked]="true" 但它似乎不起作用。任何的想法 ?或任何语法更新?谢谢
#代码
<mat-radio-group name="isSubleaseCoterminusWithMasterLease" (change)="onChangeSubleaseCoterminus($event)" [(ngModel)]="dealDispositionFormFields.isSubleaseCoterminusWithMasterLease">
<div class="flex" style="justify-content: space-between;">
<div class="deal-form-btn-group w-46-per" [ngClass]="{'v-bg-color': dealDispositionFormFields.isSubleaseCoterminusWithMasterLease === 'No'}" >
<div class="deal-form-btn-group-radio">
<mat-radio-button
[checked]='true'
*ngIf="dealDispositionFormFields.isSubleaseCoterminusWithMasterLease === 'No' else notSubleaseCoterminusWithMasterLease"
color="accent"
[value]="'No'">
<span class="alter-text-color">No</span>
</mat-radio-button>
<ng-template #notSubleaseCoterminusWithMasterLease>
<mat-radio-button
[checked]="true"
[value]="'No'">
No
</mat-radio-button>
</ng-template>
</div>
</div>
</div>
</mat-radio-group>
您可以在 <mat-radio-group>
标签内使用 ngModel
,如下所示
<mat-radio-group [(ngModel)]="selected">
并在您的 .ts 文件中
selected: string;
ngOnInit(){
this.selected = "No";
}
这是一个工作示例:Stackblitz
请问,我使用的是更新angular版本。我试图将垫子单选按钮设置为默认选中,我尝试使用 [checked]="true" 但它似乎不起作用。任何的想法 ?或任何语法更新?谢谢
#代码
<mat-radio-group name="isSubleaseCoterminusWithMasterLease" (change)="onChangeSubleaseCoterminus($event)" [(ngModel)]="dealDispositionFormFields.isSubleaseCoterminusWithMasterLease">
<div class="flex" style="justify-content: space-between;">
<div class="deal-form-btn-group w-46-per" [ngClass]="{'v-bg-color': dealDispositionFormFields.isSubleaseCoterminusWithMasterLease === 'No'}" >
<div class="deal-form-btn-group-radio">
<mat-radio-button
[checked]='true'
*ngIf="dealDispositionFormFields.isSubleaseCoterminusWithMasterLease === 'No' else notSubleaseCoterminusWithMasterLease"
color="accent"
[value]="'No'">
<span class="alter-text-color">No</span>
</mat-radio-button>
<ng-template #notSubleaseCoterminusWithMasterLease>
<mat-radio-button
[checked]="true"
[value]="'No'">
No
</mat-radio-button>
</ng-template>
</div>
</div>
</div>
</mat-radio-group>
您可以在 <mat-radio-group>
标签内使用 ngModel
,如下所示
<mat-radio-group [(ngModel)]="selected">
并在您的 .ts 文件中
selected: string;
ngOnInit(){
this.selected = "No";
}
这是一个工作示例:Stackblitz