question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ChangeDetectorRef.markAsUntouched()

See original GitHub issue

I’m submitting a…

[x] Feature request

Current behavior

Methods that don’t change anything still trigger change detection. Currently the only way to fix this is to use OnPush or detach(), both of which require modifying the entire component subtree to support OnPush change detection. E.g. A HostListener(‘scroll’) will cause change detection on each scroll, which is expensive.

Expected behavior

Methods should be able to call a method changeDetector.markAsUntouched() to indicate that the current method has not changed any state. Then, Angular can skip the onLeave change detection if only that method ran.

Minimal reproduction of the problem with instructions

  1. Create a class like
export class SomeComponent {
  aVeryLargeMutableObject = new VeryLargeMutableObject();

  @HostListener('window:scroll', [])
  onWindowScroll() {
    // Do nothing
  }
}
  1. Now, scrolling is very slow because change detection occurs every time.

What is the motivation / use case for changing the behavior?

Allow methods that don’t change anything to run efficiently, without having to modify the whole component tree to use OnPush.

Environment

Angular version: latest Browser: All browsers

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
admositycommented, Mar 6, 2018

You can also disable parts of zone’s patching globally if performance against these events are an issue:

/**
 * By default, zone.js will patch all possible macroTask and DomEvents
 * user can disable parts of macroTask/DomEvents patch by setting following flags
 */

 (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame
 (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick
 (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames

This is actually provided in newer versions of angular-cli: https://github.com/angular/devkit/blob/1f1c2750a81a9b1397327cc2a3c3cb91d18c340c/packages/schematics/angular/application/files/__sourcedir__/polyfills.ts#L55-L62

0reactions
angular-automatic-lock-bot[bot]commented, Apr 28, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ChangeDetectorRef - Angular
Base class that provides change detection functionality. A change-detection tree collects all views that are to be checked for changes. Use the methods...
Read more >
How to reset reactive Angular Material form in Angular?
I've tried to use this.formdata.reset() . It is resetting the form but it's making it touched . So I used this.formdata.markAsUntouched() ...
Read more >
Angular API Manual - Hubwiz.com
NgModule FAQs · What is the forRoot() method? ... Intercept input property changes with ngOnChanges() - Component ... changeDetectorRef · ComponentRef.
Read more >
angular/angular - Gitter
So my question is, @Output() parameters use emit() but it does not ... I've tried using NgZone and ChangeDetectorRef but without success.
Read more >
Angular 7 Dynamic validators - Medium
markAsTouched() Marks the control as touched. A control is touched by focus and blur events that do not change the value. markAsUntouched()
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found