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

269
Views
Angular 2: ¿Ignorar el primer desencadenante de ngOnChanges?

¿Hay alguna forma angular 2 de omitir el primer desencadenante de ngOnChanges? Actualmente estoy usando este enfoque ingenuo para ignorarlo:

 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

Puedes usar

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

Para agregar a la respuesta anterior y explicar esto un poco más ...

changes es una matriz de objetos que han sido modificados. Entonces, si tiene una entrada myInput , deberá acceder a ese objeto dentro de la matriz de cambios haciendo changes['myInput'] . myInput contiene:

  • previousValue - valor anterior del objeto (antes del cambio)
  • currentValue - valor actual del objeto que ha sido cambiado
  • firstChange : booleano sobre si esta es la primera vez que ha habido un cambio (tenga en cuenta que esto será verdadero cuando el componente se inicialice y falso de lo contrario) - isFirstChange() devolverá verdadero si este es el primer cambio.

Código:

 //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

Si tiene muchas entradas, pero no puede saber con certeza cuál de ellas está configurada, puede usar

  • Object.values para recuperar todos los cambios como una matriz
  • Array.some para verificar si alguno de los cambios tiene isFirstChange establecido
 ngOnChanges(changes: SimpleChanges) { const isFirstChange = Object.values(changes).some(c => c.isFirstChange()); }

Cuidado: si no se configura @Input , isFirstChange será false porque Array.some se detiene en el primer valor true .

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!