Angular 18: How to Ensure ngAfterViewInit Fires without zone.js in OnPush Components

  Kiến thức lập trình


I’ve recently removed zone.js from my Angular 18 project to optimize performance and am now facing an issue with the ngAfterViewInit lifecycle hook not firing consistently in a component with ChangeDetectionStrategy.OnPush. My current workaround involves using ApplicationRef.tick(), but I’m looking for a more appropriate solution that aligns with the reactive paradigm and does not rely on manual change detection triggers.


  • Angular version: 18.0.6
  • I’ve successfully removed zone.js, but this has led to lifecycle hooks not being triggered as expected.
  • The application structure involves an admin component that uses Angular Material’s MatSidenav, controlled via reactive signals from @angular/core.


  selector: 'app-admin',
  templateUrl: './admin.component.html',
  styleUrls: ['./admin.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
export default class AdminComponent implements OnDestroy, AfterViewInit {
  sidenav = viewChild.required<MatSidenav>(MatSidenav);

  private destroy$ = new Subject<void>();
  private observer = inject(BreakpointObserver);
  private router = inject(Router);
  private accountService = inject(AccountService);
  private cdr = inject(ChangeDetectorRef);
  private appRef = inject(ApplicationRef);
  constructor() {
      filter(e => e instanceof NavigationEnd), takeUntil(this.destroy$)
    ).subscribe(() => {

  ngOnDestroy(): void {

  ngAfterViewInit() {
      .observe(['(max-width: 800px)'])
      .pipe(takeUntil(this.destroy$), delay(1))
      .subscribe((res: { matches: boolean }) => {
        filter((e) => e instanceof NavigationEnd)
      .subscribe(() => {
        if (this.sidenav().mode === 'over') {

  logout() {

  private setSidenav(matches: boolean) {
    if (matches) {
      this.sidenav().mode = 'over';
    } else {
      this.sidenav().mode = 'side';


<mat-toolbar color="primary" class="mat-elevation-z8">
  <button mat-icon-button *ngIf="sidenav.mode === 'over'" (click)="sidenav.toggle()">
    <mat-icon *ngIf="!sidenav.opened">menu</mat-icon>
    <mat-icon *ngIf="sidenav.opened">close</mat-icon>
  <span class="admin-panel-title">Admin Panel</span>

  <mat-sidenav #sidenav="matSidenav" class="mat-elevation-z8">
    <div class="logo-container">
      <img routerLink="/" alt="logo" class="avatar mat-elevation-z8 logo-admin" src="../../../../assets/img/logo-s.png" />

    <ul class="nav-list">
      <li class="nav-item">
        <a routerLink="/admin" routerLinkActive="active" [routerLinkActiveOptions]="{exact:true}">
          <mat-icon class="nav-icon">home</mat-icon>
      <li class="nav-item">
        <a routerLink="/admin/products" routerLinkActive="active">
          <mat-icon class="nav-icon">library_books</mat-icon>
      <li class="nav-item">
        <a routerLink="/admin/brands" routerLinkActive="active">
          <mat-icon class="nav-icon">branding_watermark</mat-icon>
      <li class="nav-item">
        <a routerLink="/admin/product-types" routerLinkActive="active">
          <mat-icon class="nav-icon">branding_watermark</mat-icon>
      <li class="nav-item">
        <a routerLink="/admin/users" routerLinkActive="active">
          <mat-icon class="nav-icon">supervisor_account</mat-icon>
      <li class="nav-item">
        <a (click)="logout()" class="nav-item-logout" routerLinkActive="active">
          <i class="fa fa-sign-out fa-2x nav-icon-logout"></i>

    <div class="content mat-elevation-z8">

The ngAfterViewInit() in my AdminComponent only logs when I explicitly call this.appRef.tick(). However, I would prefer not to use ApplicationRef for this purpose and am looking for a solution that allows Angular to handle updates more naturally without reverting to zone.js.


  • The ngAfterViewInit() method is not triggering as expected without manual intervention via ApplicationRef.tick().
  • This issue occurs only after removing zone.js, indicating a challenge with Angular’s change detection mechanism in a zone-less environment.

How can I ensure that lifecycle hooks like ngAfterViewInit are triggered appropriately in an Angular application with ChangeDetectionStrategy.OnPush when zone.js is removed? Are there any recommended practices or patterns for managing change detection manually in such cases?

What I’ve Tried:

  1. Using ApplicationRef.tick() to manually trigger change detection.
  2. Subscribing to router events and calling ChangeDetectorRef.detectChanges() within subscriptions.
  3. Using for executing code that updates the view.

None of these methods have seamlessly integrated into the reactive architecture I aim for. I’m looking for a more integrated or Angular-recommended approach that enhances performance without compromising reactivity and maintainability.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website