angular - 提交后如何禁用Angular反应表单中的按钮
问题描述
我正在使用角度反应形式,如何在单击/提交后禁用按钮并更改按钮标签?
解决方案
Angular 没有在表单上提供“提交的表单”属性。最好的方法是在提交表单时更新一个布尔值(您在模板中使用)并在适当的时候重置它。
以下示例使用响应式表单并使用表单数据伪造 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 ... }
);
}
推荐阅读
- android - Gradle 错误:ProductFlavor 名称不能以“test”开头
- java - 将二维数组写入 Google 电子表格
- axios - 尝试使用 Axios / node-fetch 将 apk 文件上传到 Browserstack 云时出错
- google-maps - Xamarin.Forms.GoogleMaps 动画图钉
- python - ImportError:使用 SWIG 时 _...so 文件中的未定义符号
- c++ - 在 MENUITEMINFO 上创建位图不会加载位图
- php - 如何在 prestashop 模板中获取类别描述
- automation - 无法创建新的远程会话
- xml - 使用 XSLT 将 XML 元素名称转换为 camelCase 格式
- .net-core - 无法在 CI 服务器中运行 dotnet build。无法解析 nuget 包