检测 Angular 形式(非响应式)中的数据是否已更改 [英] Detect if data in an Angular form (not reactive) was changed

查看:27
本文介绍了检测 Angular 形式(非响应式)中的数据是否已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Angular 形式(非反应式),数据绑定在 ngModel 中:

 
<input [(ngModel)]="user.name"><input [(ngModel)]="user.color"><button type="submit">保存</button></表单>

如果绑定数据未更改,如何禁用提交按钮?

解决方案

您可以检查脏标志,它告诉您表单是否脏.

如果您更改其中的某些值,表格就会变脏.

点击此处了解更多详情:

I have Angular form (not reactive), with data binded in ngModel:

 <form #form="ngForm">
  <input [(ngModel)]="user.name">
  <input [(ngModel)]="user.color">

  <button type="submit">Save</button>
 </form>

How can i disable submit button if binded data has not been changed?

解决方案

You can check the dirty flag, which tells you if the form is dirty or not.

<button type="submit" [disabled]="!form.dirty">Save</button>

The form becomes dirty if you change some value in it.

Check here for more details: https://angular.io/guide/forms

这篇关于检测 Angular 形式(非响应式)中的数据是否已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆