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

461
Views
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
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!