Skip to content

Instantly share code, notes, and snippets.

@dpsthree
Last active February 17, 2020 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpsthree/ad8f387c763858c69054026f13924e04 to your computer and use it in GitHub Desktop.
Save dpsthree/ad8f387c763858c69054026f13924e04 to your computer and use it in GitHub Desktop.
import {
Component, Input, AfterViewInit,
ChangeDetectionStrategy, ChangeDetectorRef
} from '@angular/core';
@Component({
selector: 'app-instructor-list',
templateUrl: './instructor-list.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class InstructorListComponent implements AfterViewInit {
// Suppose this time the instructor list doesn't change after it arrives
@Input() instructors = [];
constructor(private cdr: ChangeDetectorRef) { }
// Wait until the view inits before disconnecting
ngAfterViewInit() {
// Since we know the list is not going to change
// let's request that this component not undergo change detection at all
this.cdr.detach();
}
// Angular provides additional controls such as the following
// if the situation allows
// Request a single pass of change detection for the application
// this.cdr.markForCheck();
// Request a single pass of change detection for just this component
// this.cdr.detectChanges();
// Connect this component back to the change detection process
// this.cdr.reattach();
}
@Argee88
Copy link

Argee88 commented Feb 17, 2020

this.crd.detach(); should be this.cdr.detach(); ;)

@dpsthree
Copy link
Author

Fixed, thanks!

@Argee88
Copy link

Argee88 commented Feb 17, 2020

Welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment