Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
Angular 2: Ignore first trigger of ngOnChanges?

Is there any angular 2 way to skip the first trigger of ngOnChanges? Currently I am using this naive approach to ignore it:

isFirst: boolean = true;

  ngOnChanges(changes: SimpleChanges) {
    if (this.isFirst) {
      this.isFirst = false;
      return;
    }
    console.log(changes);
  }
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can use

https://angular.io/docs/ts/latest/api/core/index/SimpleChange-class.html#!#isFirstChange-anchor

if(changes['prop'].isFirstChange()) {
}
about 4 years ago · Santiago Trujillo Report

0

To add onto the previous answer and explain this a bit more...

changes is an array of objects that have been changed. So if you have an input myInput, you'll need to access that object within the changes array by doing changes['myInput']. myInput contains:

  • previousValue - previous value of the object (before the change)
  • currentValue - current value of the object that has been changed
  • firstChange - boolean on whether this is the first time there has been a change (note this will be true when the component intializes and false otherwise) - isFirstChange() will return true if this is the first change.

Code:

//your input
@Input() myInput: any;

ngOnChanges(changes: any) {
  //check if this isn't the first change of myInput
  if(!changes['myInput'].isFirstChange()) {
    //do something
  }
}
about 4 years ago · Santiago Trujillo Report

0

If you have many inputs, but can't tell for sure which one of them is set, you may use

  • Object.values to retrieve all changes as an array
  • Array.some to check whether any of the changes has isFirstChange set
ngOnChanges(changes: SimpleChanges) {
    const isFirstChange = Object.values(changes).some(c => c.isFirstChange());
}

Beware: If no @Input is set at all, isFirstChange will be false because Array.some stops at the first true value.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!