1

I created an intro/splash screen component that should display for 5 seconds and then automatically navigate to the login page.

The strange thing is:

  • If I don’t use setTimeout, Angular correctly renders the content of the component’s HTML template.

  • As soon as I wrap the navigation in a setTimeout (or any asynchronous operation), the page stays completely blank — nothing from the template is displayed.

TS file:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-introduction-page',
  imports: [],
  templateUrl: './introduction-page.html',
})
export class IntroductionPage implements OnInit{
  private intervalId: any;

  constructor(private router: Router) {
  }

  ngOnInit(): void {
    setTimeout(() => {
      this.router.navigate(['/login'])
      
    }, 5000);}
}

HTML file:

<p>Hello</p>
New contributor
Giorgio Bonacorsi is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • This code itself working fine for me Commented yesterday

3 Answers 3

0

The splash screen renders blank because Angular can’t detect changes inside your setTimeout() callback (it runs outside Angular’s zone). Wrap the navigation inside NgZone.run():

import { Component, OnInit, NgZone } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-introduction-page',
  templateUrl: './introduction-page.html',
})
export class IntroductionPage implements OnInit {
  constructor(private router: Router, private ngZone: NgZone) {}

  ngOnInit(): void {
    setTimeout(() => {
      this.ngZone.run(() => {
        this.router.navigate(['/login']);
      });
    }, 5000);
  }
}

Alternatively, use Angular’s built-in timer() from RxJS instead of setTimeout().

Sign up to request clarification or add additional context in comments.

Comments

0

Try to run the setTimeout inside Angular's zone to ensure proper change detection: just as suggested above

import { Component, OnInit, NgZone } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-introduction-page',
  imports: [],
  templateUrl: './introduction-page.html',
})
export class IntroductionPage implements OnInit {
  constructor(private router: Router, private ngZone: NgZone) {}

  ngOnInit(): void {
    this.ngZone.run(() => {
      setTimeout(() => {
        this.router.navigate(['/login']);
      }, 5000);
    });
  }
}
New contributor
Joseph Ajayi is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Comments

0

Your component is missing standalone: true.

Without it, Angular does not treat the component as a standalone component, so the template is never rendered. It may look like it works until you introduce any async operation (like setTimeout), then Angular never runs change detection for that view and you get a blank page.

@Component({
  selector: 'app-introduction-page',
  standalone: true,
  imports: [],
  templateUrl: './introduction-page.html',
})
export class IntroductionPage implements OnInit {
  constructor(private router: Router) {}

  ngOnInit(): void {
    setTimeout(() => this.router.navigate(['/login']), 5000);
  }
}

If you’re not using standalone components, then make sure this component is declared in a module instead.

New contributor
BDK Web is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Comment

from Angular 19, the components are standalone by defect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.