3

I am using angular 4 and i am checking is any value is changed by user or not? In my page there are more than 100 control(textbox ,dropdown and editable grid)

I need to check only if any value is changed or not. For that i am using below code but it is called multiple times while pageload.

How i can avoid this.i need valuechange will execute only on when user change any value.

import { Component, OnInit, ViewChild, AfterViewInit, HostListener, OnChanges, DoCheck, AfterViewChecked,OnDestroy } from "@angular/core";


 @ViewChild('fromNote') fnotes;
 
 ngAfterViewInit() {
        this.fnotes.valueChanges.subscribe(val => {
            alert('changed');
        });
}

2
  • Do .next() on valueChanges of fnotes, only when the user changes the value. So the subscribe method gets the value when the use changes the value. Commented May 10, 2018 at 11:58
  • Would you provide HTML code where you have use #fromNote? Commented May 10, 2018 at 12:01

4 Answers 4

3

In Angular 5 you’ll be able to specify the update mode for forms and form controls.

updateOn: 'submit' runs the value changes only when the form is submitted.

this.nameForm = new FormGroup ({
  firstname: new FormControl('', {
    validators: Validators.required,
    updateOn: 'submit'
  }),
  lastname: new FormControl('', {
    validators: Validators.required,
    updateOn: 'submit'
  })
});

Different options are updateOn: 'blur' and updateOn: 'submit'

const c = new FormControl('', { updateOn: 'blur' }); and

 const c = new FormControl('', { updateOn: 'submit' });

From Official Documentation updateOn to 'submit', will delay value and validity updates until the parent form of the control fires a submit event.

Sign up to request clarification or add additional context in comments.

1 Comment

@pushp, have you checked this?
0

According to the official documentation, ngOnChanges gets called by default before the ngOnInit as well as on data-bound input properties change.

So, if you want to avoid that, try to:

  • Limit the @Input bound property changes on page load
  • Use a flag to detect only the user-made changes :

for instance:

private initialised = false;

ngInInit(){
  ...
  this.initialized = true;
}

ngOnChanges(){
  if(this.initilazied){<your code here>}
}

Or: use a different lifecylce hook, or use the (change) method calls in the component

Comments

0
            import { Component, OnInit, ViewChild, AfterViewInit, HostListener, OnChanges, DoCheck, AfterViewChecked,OnDestroy } from "@angular/core";

            @ViewChild('fromNote') fnotes;
            public subScription;
            ngAfterViewInit() {
                 this.subScription=  this.fnotes.valueChanges.subscribe(val => {
                     alert('changed');
                 });
            }

            ngOnDestroy() {
                 this.subScription.unsubscribe()
            }

Are you trying to unsubscribe to the valuechanges in ngOnDestroy ? If not try to unsubscribe. Valuechanges is an observable and needs to be unsubscribed or it will look for changes in every instance.

Comments

0
 import { Component, OnInit, ViewChild, AfterViewInit, HostListener, OnChanges, DoCheck, AfterViewChecked,OnDestroy } from "@angular/core";
 @ViewChild('fromNote') fnotes;
 
 ngAfterViewInit() {
        this.fnotes.valueChanges.subscribe(val => {

    if(this.fnotes.dirty){
        alert('changed');
    }
        });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.