Detect if data in form was changed

青春壹個敷衍的年華 提交于 2019-11-28 01:51:50

do this , check for dirty flag, which tells form dirty or not

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

form becomes dirty if you change some value in it.

Check here for detail : https://angular.io/guide/forms

According to your comment 'But what if i erase 1 symbol in input and then wright it again (the value is the same, but form was changed)?' I suggest this solution.

The general idea is to store the initial value of the form as separate object (just cloning it). And then create a boolean function which simply iterate through the key-values and compare Updated data with the Initial data. After this just bind the result of this function to your submit button [disabled]="yourCheckMethod(form.value)".

You can try it with the pristine property like this:

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

This property checks if your form has changed since it was loaded.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!