Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

323
Vistas
How to get previous value when component has been destroyed in Angular

I have component and render it in cycle (ngFor) The component has an object

@Input() a = {
name: 'Bob'
}

After the change, it will reset its input parameters to the initial state When the component has changed, I want to get the values ​​that were at its initial initialization SCENARIO: -> @Input() a = {name: 'Bob'} (init value) -> then do something... this.a.name = 'Alice'; -> in the loop, the object changes -> Once again, the component matters @Input() a = {name: 'Bob'} (init value) I want to get the previous value after the component has been updated that is 'Alice'

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

From the angular documentation:

ngOnChanges(changes: SimpleChanges) {
  if(changes.a) {
    let chng = changes.a;
    let cur  = JSON.stringify(chng.currentValue);
    let prev = JSON.stringify(chng.previousValue);
  }
}

So, you'll need the .currentValue and .previousValue property to access current and previous values.

Edit:1

If the component gets destroyed you have to use some kind of state management - service with subject, sessionStorage, localStorage or something else

Edit: 2

You can extend the ngFor directive and add the state logic inside ngOnChanges.

@Directive({
  selector: '[ngFor][ngForIn]'
})
export class NgForIn extends NgFor implements OnChanges {

  @Input() ngForIn: any;

  constructor(viewContainer: ViewContainerRef,
              template: TemplateRef<NgForRow>,
              differs: IterableDiffers,
              cdr: ChangeDetectorRef) {

    super(viewContainer, template, differs, cdr);
  }

  ngOnChanges(changes: SimpleChanges): void {
    // Do something here
  }

}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda