问题描述: 在Angular中,当验证表单后,表单按钮无法启用。
解决方法:
[disabled]="!form.valid"
,这将根据表单的有效性来启用或禁用按钮。
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.css']
})
export class FormComponent {
// 表单模型
model: any = {};
onSubmit(form: NgForm) {
if (form.valid) {
// 执行提交表单的操作
}
}
}
在上述代码中,onSubmit
方法中的form.valid
将返回表单的有效性。只有当表单有效时,按钮才会启用。
通过以上步骤,您可以在验证表单后启用Angular表单按钮。