Codeground AI
EditorWorkspacesCoursesInterviews Ground Call New Daily Challenges
AI tools
  • Resume De-AI-ifierHumanize AI-written resumes & flag clichés
  • AI Email RewriterMake robotic AI emails sound human
  • AI Code DetectorSpot AI-generated or copied code
  • AI Watermark ToolStrip or embed invisible text watermarks
  • README GeneratorCreate, edit & preview GitHub READMEs
  • SQL AssistantExplain SQL or generate from English
  • Text SummarizerCondense articles and notes instantly
  • AI Text GeneratorDraft blogs, emails, and product copy
  • System Prompt DiffCompare prompts for behavior & risk deltas
  • AI Code SecurityScan for secrets, SQLi, SSRF & more
  • Commit Message GeneratorConventional commits from a git diff
  • Interview Question PackJD → coding, behavioral & system design Qs
  • Schema Seed DataTS/SQL schema → realistic JSON fixtures
  • GraphQL ExplainerExplain queries and debug GraphQL errors
Data & format
  • JSON DiffCompare two JSON blobs side by side
  • Diff & PatchGenerate unified patches from text/code
  • JSON FormatterPretty-print and validate JSON
  • SQL FormatterFormat SQL and explain with AI
  • JSON ↔ CSVConvert tabular data both ways
  • Base64 CodecEncode and decode Base64
  • Log ParserPretty-print logs and highlight severity
  • Protobuf DecoderDecode Base64 or hex protobuf payloads
Security & web
  • JWT DebuggerDecode and verify JSON Web Tokens
  • ENV LinterLint .env files and redact values
  • Password GeneratorStrong, configurable passwords
  • UUID GeneratorGenerate UUID v1/v4 in bulk
  • Regex TesterTest patterns in real time
Generators & utilities
  • Epoch ConverterConvert between Unix and dates
  • Meeting PlannerMatrix of slots across timezones
  • Date MathAdd duration with timezone awareness
  • Cron BuilderValidate cron and preview next runs
  • QR GeneratorMake scannable QR codes
  • Color PickerPick & convert colors
  • Lucky Draw WheelSpin-the-wheel utility
Network & creative
  • Speed TestMeasure network throughput
  • Diagram StudioFlowcharts & architecture diagrams
  • Canvas DrawingA scratchpad for sketches
  • Turtle GameCoding game for kids
See everything Codeground AI offers
ReadsGames
Sign In Sign Up
EditorWorkspacesCoursesInterviewsGround CallDaily ChallengesReadsGames
Tools
Resume De-AI-ifierAI Email RewriterAI Code DetectorAI Watermark ToolREADME GeneratorSQL AssistantText SummarizerAI Text GeneratorSystem Prompt DiffAI Code SecurityCommit Message GeneratorInterview Question PackSchema Seed DataGraphQL ExplainerJSON DiffDiff & PatchJSON FormatterSQL FormatterJSON ↔ CSVBase64 CodecLog ParserProtobuf DecoderJWT DebuggerENV LinterPassword GeneratorUUID GeneratorRegex TesterEpoch ConverterMeeting PlannerDate MathCron BuilderQR GeneratorColor PickerLucky Draw WheelSpeed TestDiagram StudioCanvas DrawingTurtle Game

Sign InSign Up

Notifications 0

Lazy Loading in Angular

Ashutosh Singh - March 24, 2025


Create the Lazy-Loaded Component


In the application, add a new component using the Angular CLI command, as shown below.

ng g c hello --flat --skip-import

Here --skip-import flag is used so that Angular does not declare HelloComponent in the module, as you wish to load HelloComponent dynamically.

Next, add code in HelloComponent as shown in the next code listing:


import { Component, OnInit, EventEmitter, Output, Input } from '@angular/core';

@Component({
  selector: 'app-hello',
  template: `
  <h1>Greetings</h1>
  <h2>{{helloMessage}}</h2>
  <button (click)='hello()'>sayHello</button>
  `

})
export class HelloComponent implements OnInit {

  @Input() helloMessage: string;
  @Output() sendMessageEvent = new EventEmitter();
    
  constructor() { }
  ngOnInit(): void {
  }

  hello(): void {
    this.sendMessageEvent.emit('Hello From Hello Component');

  }
}



HelloComponent has an @Input() decorated property to accept data from the parent component and an @Output() decorated EventEmitter so that the event raised here can be handled in the parent component. The parent component is a component in which HelloComponent will be loaded dynamically.



Lazy Load in the Parent Component


You can lazily load a component in any other component, hence creating a parent-child relationship between them. You want to lazy load HelloComponent on the click of the button in the parent component, so to do that add a button as shown next.


<h1>{{message}}</h1>
<button (click)='loadHelloComponent()'>Hello</button>



Next, in the constructor of the parent component inject ViewContainerRef and ComponentFactoryResolver classes:


 constructor(private vcref: ViewContainerRef,
    private cfr: ComponentFactoryResolver){ }



As of the official documentation, ComponentFactoryResolver class is a simple registry that maps components to generated ComponentFactory classes that can be used to create an instance of the component using the create() method.

The ViewContainerRef represents a container where one or more views can be attached. It can contain host views by instantiating a component with the createComponent() method.

To lazy load the component, we will use the import() method inside an async/await function.




async loadHelloComponent(){
    
    this.vcref.clear();
    const { HelloComponent } = await import('./hello.component');
    let hellocomp = this.vcref.createComponent(
      this.cfr.resolveComponentFactory(HelloComponent)
    );
  
  }


The above function first clears the container; otherwise, on every click of the button, the new instance of HelloComponent would be added in the container. After that, the function imports the HelloComponent using the import method. By using await syntax, it loads the component asynchronously. Once the reference of HelloComponent is loaded, it creates the component on the view container using the createComponent method and bypassing resolveComponentFactory method in it.

Now when you click the button, you will lazy load the HelloComponent inside the parent component.


Passing Data to Lazy-Loaded Component


Angular allows us to pass data to @Input() decorated properties and handle events of lazy-loaded components using the instance property of the lazy-loaded component. For example, you can pass data and handle an event of HelloComponent in the parent component as shown in the next code listing:


hellocomp.instance.helloMessage = "Data Passed from Parent";
    hellocomp.instance.sendMessageEvent.subscribe(data=>{
      console.log(data);
    })



As you see, you can use instance to access the properties and events of the lazy-loaded component.


Using then() Instead of await


Sometimes it is not possible to make a function async. Hence, you cannot use an await statement as we did earlier. In this scenario you can use promise’s then method to lazy load a component as shown in the next code listing:

  loadHelloComponent() {
    this.vcref.clear();
    import('./hello.component').then(
      ({ HelloComponent }) => {
        let hellocomp = this.vcref.createComponent(
          this.cfr.resolveComponentFactory(HelloComponent)
        );
        hellocomp.instance.helloMessage = "Data Passed from Parent";
        hellocomp.instance.sendMessageEvent.subscribe(data => {
          console.log(data);
        })
      }
    )
  }


In the above function, everything is the same. It just promises the then method is used instead of async-await statements.


Using a Module in the Lazy-Loaded Component


Sometimes a lazy-loaded component relies on other modules. For example, let’s say HelloComponent is using [(ngModel)] as shown in the next code listing:

 template: `
  <h1>Greetings</h1>
  <h2>{{helloMessage}}</h2>
  <input type='text' [(ngModel)]='message' />
  <h3>Hello {{message}}</h3>
  <button (click)='hello()'>sayHello</button>
  `

HTML

As ngModel is the part of FormsModule, when you use it inside a lazy-loaded component, Angular complains about that with error: Can’t bind to ngModel since it isn’t a known property of input.

This problem can be fixed by importing FormsModule inside the HelloComponent itself. With that inside the same file hello.component.ts, create a module and pass FormsModule inside the imports array as shown in the next code listing:

@NgModule({
  declarations: [HelloComponent],
  imports: [FormsModule]
})
class PlanetComponentModule {}

TypeScript

You have to create ngModule in the same file in which the component is created and pass all the dependent modules in the imports array.



Lazy Loading Components in ng-template


To lazy load a component inside a particular location in the template, you can use ViewChild. Let’s say you wish to lazy load HelloComponent inside the #hellotemp template of the parent component.

<div>
    <ng-template #hellotemp></ng-template>
</div>

HTML


To do this, refer hellotemp as a ViewChild in the parent component class as shown in the next code listing:

  @ViewChild('hellotemp', { read: ViewContainerRef })
  private helloviewcontainerref: ViewContainerRef;

TypeScript



Here we are reading the ng-template as ViewContainerRef so that the component can be loaded to it, and finally, you can lazy load a component inside it as we did earlier:

async loadHelloComponent(){

    this.vcref.clear();
    const { HelloComponent } = await import('./hello.component');
    let hellocomp = this.helloviewcontainerref.createComponent(
      this.cfr.resolveComponentFactory(HelloComponent)
    );
    hellocomp.instance.helloMessage = "Data received from patient";
    hellocomp.instance.sendMessageEvent.subscribe(data=>{
      console.log(data);
    })

  }





Codeground AI

The browser is the only IDE you need. Interactive courses, cloud workspaces, 15+ language runtimes, secure interview tooling and a polished developer toolbox — all in one tab.

Languages

  • Node.js
  • Python
  • Java
  • C++
  • Go
  • Rust
  • TypeScript
  • React (JSX)
  • Web (HTML/CSS/JS)
  • Shell / Bash
  • Ruby
  • Lua
  • Groovy

Databases

  • MongoDB
  • PostgreSQL
  • MySQL
  • Redis
  • ClickHouse

AI Tools

  • Resume De-AI-ifier
  • AI Email Rewriter
  • AI Code Detector
  • AI Watermark Tool
  • README Generator
  • SQL Assistant
  • Text Summarizer
  • AI Text Generator
  • System Prompt Diff
  • AI Code Security
  • Commit Message Generator
  • Interview Question Pack
  • Schema Seed Data
  • GraphQL Explainer

Data tools

  • JSON Diff
  • Diff & Patch
  • JSON Formatter
  • JSON ↔ CSV
  • SQL Formatter
  • SQL Query Generator
  • Log Parser
  • Protobuf Decoder

Utilities

  • JWT Debugger
  • Base64 Codec
  • Regex Tester
  • Epoch Converter
  • Current Unix Time
  • Timestamp Diff
  • Cron Builder
  • Meeting Planner
  • ENV Linter
  • Date Math
  • QR Generator
  • UUID Generator
  • Color Picker
  • Password Generator
  • Speed Test
  • Diagram Studio
  • Canvas Drawing
  • Lucky Draw Wheel

Platform & company

  • All Tools
  • Courses
  • Daily Challenges
  • Interviews
  • Reads
  • Games
  • Turtle (Kids)
  • About Us
  • Privacy Policy
  • Sitemap
  • Contact

© 2026 Codeground AI. Built for developers who want to ship.

About·Privacy·Sitemap·[email protected]