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

471
Visualizações
How can i put some code into ngZone back?

I have scroll event and always when i am scrolling change detection is running in background

So because of that i tried to optimzie my scrolling with debouncing

The idea is: while i am scrolling initially i put my code in runOutsideAngular which will not trigger the changed detection BUT after that intervall in the deboucincing is passed i want to runIn angular zone - but it is not happening - my number is not updated in the UI

HTML

<h1>{{ number }}</h1>



<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>
<h1>sdsdsd</h1>

<h1>sdsdsd</h1>

APP COMPONENT TS

constructor(public ngZone: NgZone) {};

  codeToExecute() {
    this.ngZone.run(() => {
      this.changeSomethingToUI();
    })
  }

  ngOnInit() {
      const processChange = this.debounce(this.codeToExecute.bind(this), 1000);

      this.ngZone.runOutsideAngular(() => {
        window.addEventListener('scroll', processChange)
      })
  
  }

  debounce(fn, delay = 300){
    let timer;
    const that = this;
    return function() {
      clearTimeout(timer);
      timer = setTimeout(() => {
        fn();
      }, delay)
    }
  }

  number = 5;
  changeSomethingToUI() {
    this.number = 15;
  }

  ngAfterViewChecked() {
    console.log('Change detection triggered!');
  }

why after i stop scrolling in 1000ms i don't get my number updated to 15 ?

about 4 years ago · Juan Pablo Isaza
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