Welcome to the Angular Tutorial! Today, we're diving into the heart of Angular - Modules. 💡
In Angular, a module is a container for components, directives, pipes, and services. It helps in organizing and managing your application in a more efficient and scalable way.
To create an Angular module, we use the @NgModule decorator. Let's create a simple AppModule.
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }Here's a breakdown of the @NgModule decorator:
declarations: Components, directives, and pipes that belong to this module.imports: Modules that this module depends on.providers: Services that this module provides.bootstrap: The component that this module starts with.Let's create a HeroModule that contains a HeroComponent for displaying a list of superheroes.
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HeroComponent } from './hero.component';
@NgModule({
declarations: [HeroComponent],
imports: [CommonModule],
exports: [HeroComponent]
})
export class HeroModule { }In this example, we're using CommonModule for common directives like ngFor and ngIf. We also added exports: [HeroComponent] to allow other modules to use the HeroComponent.
What is the main purpose of an Angular module?
In the next lesson, we'll explore components in detail. Stay tuned! 💡