场景:
我用于单选按钮的代码
<div class="form-group m-form__group row">
<div class="m-radio-list">
<mat-radio-group>
<p>
<mat-radio-button value="newBusiness">New</mat-radio-button>
<mat-radio-button value="renewal">Renewal</mat-radio-button>
<mat-radio-button value="rollover">Old</mat-radio-button>
</p>
</mat-radio-group>
</div>
</div>
Todo:
答案 0 :(得分:0)
尝试这样
<div class="radio">
<label>
<input type="radio" name="radio" value="New" (click)="setradio('New')" [checked]='true' ngModel>
New
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio" value="Renewal" (click)="setradio('Renewal')" ngModel>
Renewal
</label>
</div>
<div *ngIf="isSelected('New')" >
<input type="text"/> New radio button selected
</div>
<div *ngIf="isSelected('Renewal')">
<input type="text"/> Renewal radio button selected
</div>
在您的组件中。
private selectedLink: string="New";
setradio(e: string): void {
this.selectedLink = e;
}
isSelected(name: string): boolean {
if (!this.selectedLink) { // if no radio button is selected, always return false so every nothing is shown
return false;
}
return (this.selectedLink === name); // if current radio button is selected, return true, else return false
}
答案 1 :(得分:0)
Demo with Bydefault a checkbox selected
应用代码: https://stackblitz.com/edit/angular-27et6e?file=src/app/app.component.ts
(在此代码中,默认情况下,我选择了第三个复选框,如果需要,可以选择第一个复选框)
方法:
radio-buttons
,另一个用于根据所选的radio-button
显示相应的输入容器。change
之类的 (change)="changeComboo($event)"
事件,ngModel
来绑定值到[(ngModel)]="favoriteSeason"
之类的变量。
更新1(默认选中第一个复选框):
mat-radio-group
是使用[(ngModel)]="favoriteSeason"
绑定的,因此ngOnint()
中,我们可以通过favoriteSeason
将此this.favoriteSeason = this.seasons[0];
变量分配为默认值 (如果要选择第一个复选框) 。