首页 > 解决方案 > 角度单选按钮重置无法正常工作

问题描述

我正在为我的表单使用下面的单选按钮

<input type="radio" class="form-check-input" value="1" [checked]="true" name="keys" [(ngModel)]="lostkey.key">One
<input type="radio" name="keys" value="2" [(ngModel)]="lostkey.key" class="form-check-input">Both

我使用以下代码将“一个”单选按钮设置为默认值

ngOnInit() {
    this.lostkey.key = '1';
}

这是我的重置按钮。这运作良好。所有表格重置。但问题是,默认单选按钮也未选中。因此我在表格重置后重新分配它。但它没有用。

重置按钮(这是正常工作)

<button type="button" class="btn btn-action btn-flat float-right"
    (click)="resetForm(reportlostkeyform)">
    <i class="fas fa-redo"></i> <span> Reset</span>
</button>

这是重置功能

resetForm(reportlostkeyform: NgForm) {
    reportlostkeyform.reset();
    this.lostkey.key = '1';
}

我如何重新分配此单选按钮值

标签: angular

解决方案


尝试重置 setTimeout 中的值;

    resetForm(reportlostkeyform: NgForm) {
        reportlostkeyform.reset();
        setTimeout(() => {
        this.lostkey.key = '1';
       })    
    }

推荐阅读