Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda