[英]How to disable button in Angular reactive form after submit
我正在使用角度反应形式,如何在单击/提交后禁用按钮并更改按钮标签?
Angular does not provide a "form submitted" property on forms. Angular 没有在表单上提供“表单提交”属性。 The best way would be to update a boolean (that you use in the template) when submitting the form and reset it when appropriate.
最好的方法是在提交表单时更新一个布尔值(您在模板中使用的)并在适当的时候重置它。
The following example uses reactive forms and fakes a POST with the form data.以下示例使用响应式表单并使用表单数据伪造 POST。
<form [formGroup]="myForm"
(ngSubmit)="onSubmit()">
... form stuff ...
<button type="submit"
[disabled]="isSubmitting">
{{isSubmitting ? 'Submitting...' : 'Submit'}}
</button>
</form>
onSubmit(): void {
// catch if the form has errors
if (this.myForm.invalid) return;
// if no errors, continue with the submit
this.isSubmitting = true;
this.http
.post('www.example.com', this.myForm.value)
.subscribe(
success => { this.isSubmitting = false },
error => { ... handle the error ... }
);
}
you can create a local variable and make it true when ever submit action happen.您可以创建一个局部变量并在提交操作发生时使其为真。 Also you can put variable name in button
您也可以将变量名称放在按钮中
<!-- html file->
<form [formGroup]='myForm' (ngSubmit)='onSubmit()'>
..........
.........
<button type="submit" [disabled]="makeDisable"> {{btnName}} </button>
// Ts file
btnName = 'submit'; // initial label
makeDisable = false;
onSubmit() {
this.makeDisable = true;
this.btnName = 'label changed'; // change the label
// make API call If every thing is done put this code in async callbabck
this.makeDisable = false;
this.btnName = 'submit'; // restore the label
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.