myHotTake

What Do @Component and @NgModule Actually Do in Angular?

If you find this story helpful, feel free to give it a like or share it with someone diving into Angular!


Imagine I’m building a city. Every building in my city needs to serve a purpose: some are homes, some are schools, and others are supermarkets. But just slapping bricks together doesn’t make a building useful—it needs a signboard to tell people what it is and how it functions. That’s where decorators like @Component and @NgModule come in. They’re the signboards that turn raw structures into purposeful entities.

When I use @Component, it’s like putting up a sign that says, “This building is a house! It has a living room, bedrooms, and a kitchen. Here’s the blueprint (template) and interior design (styles) to define how it looks and works.” Without this sign, my house would just be an anonymous block of cement and walls, and no one would know what to do with it.

Now, @NgModule is on another level—it’s like zoning the city into districts. This decorator says, “All the houses, schools, and supermarkets in this zone belong to the same neighborhood.” It also manages utilities, like deciding which roads (services) or connections (dependencies) should be available in this neighborhood. Without @NgModule, my city would be chaotic, with no clear organization or infrastructure.

So, decorators in Angular are the labels and zoning laws that turn ordinary code into a meaningful, functional application. With them, everything in my city—er, app—is clear, purposeful, and easy to navigate.


In our Angular “city,” the decorators @Component and @NgModule are the tools that give structure and meaning to our app. Now let’s unpack how they work with some code examples.

Example of @Component: Building a House

In JavaScript/TypeScript, a class is like the raw structure of a house. It’s just bricks and walls until we label it with @Component:

import { Component } from '@angular/core';

@Component({
  selector: 'app-house',
  template: `
    <h1>Welcome to My House</h1>
    <p>This is the living room</p>
  `,
  styles: [`
    h1 { color: blue; }
    p { font-size: 14px; }
  `]
})
export class HouseComponent {
  // Logic for the house goes here
  turnOnLights() {
    console.log('Lights are on!');
  }
}

Here’s what’s happening:

  1. The @Component decorator “labels” this class as a house in our Angular city.
  2. The selector ('app-house') defines where this house will appear in the DOM.
  3. The template is the blueprint for the house’s design, and the styles add a bit of flair.

Without @Component, this class would just be another plain structure. The decorator ties it to Angular’s ecosystem and makes it functional.


Example of @NgModule: Zoning the City

An Angular module organizes these components into neighborhoods. Here’s how we do it:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HouseComponent } from './house.component';

@NgModule({
  declarations: [HouseComponent], // Register the components in this neighborhood
  imports: [BrowserModule], // Bring in other zones or utilities
  bootstrap: [HouseComponent] // Kickstart the app with HouseComponent
})
export class AppModule {}

What’s happening here:

  1. The @NgModule decorator groups components (like HouseComponent) into a “zone” or logical neighborhood.
  2. declarations lists all the buildings in this neighborhood.
  3. imports brings in utilities from other zones, like BrowserModule (which enables running Angular in a browser).
  4. bootstrap specifies the first building visitors see when they enter this zone.

Without @NgModule, there’s no way to organize or structure the city’s layout, leaving everything in disarray.


Key Takeaways

  1. @Component: This decorator labels a class as a specific “building” in Angular’s city, tying it to templates, styles, and logic.
  2. @NgModule: This decorator organizes “buildings” into neighborhoods, managing their relationships and dependencies.
  3. Together, these decorators transform raw JavaScript/TypeScript classes into functional, maintainable pieces of an Angular app.

Final thought: Angular decorators are like city planning—they make sure every building serves its purpose and that the city functions as a whole. Understanding these basics will help you confidently navigate Angular’s ecosystem and build scalable apps. 🚀