How to check for changes in form in Angular 2 using

前端 未结 10 713
轻奢々
轻奢々 2020-12-08 13:53

I have a form with few data fields and two buttons.I want to enable the buttons only if the user makes some changes to the form. I have tried using:

this.for         


        
10条回答
  •  萌比男神i
    2020-12-08 14:37

    You can compare your object against the result of the form when submitting

    let changes = false;
    for ( let [ key, value ] of Object.entries( this.form.value ) ) {
        const form = this.form.value;
        const record = this.record;
        if ( form[ key ] != record[ key ] ) {
            changes = true;
            break;
        }
    }
    if ( !changes ) {
        // No changes
    } else {
        this.record = this.form.value;
        this.UpdateRecord();
    }
    

提交回复
热议问题