Module
A Module is a container in an Angular Application. This is used to organise and structure the application. Also, it helps in managing different dependencies and configurations used in the application. We have multiple modules in an angular application and the main one is AppModule where we have to configure most of the things. Modules help in modularity, lazy loading and maintainability.
Syntax: @NgModule
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ // Declare all other components,directives,pipes here declarations: [AppComponent], // Add modules here imports: [BrowserModule], // Provide services providers: [], // Root component that is used as // entry point of your application bootstrap: [AppComponent], }) export class AppModule { }
Example: Below is the app module where every component, service, etc are organised.
Javascript
// app.module.ts import { NgModule } from '@angular/core' ; import { BrowserModule } from '@angular/platform-browser' ; import { AppComponent } from './app.component' ; // Import a service import { EmployeeService } from './employee.service' ; import { EmployeeDetailsComponent } from './employee-details/employee-details.component' ; import { EmployeeListComponent } from './employee-list/employee-list.component' ; @NgModule({ declarations: [AppComponent, EmployeeDetailsComponent, EmployeeListComponent,], imports: [BrowserModule], providers: [EmployeeService], // Provide services bootstrap: [AppComponent], }) export class AppModule { } |
What is the difference between Service Directive and Module in Angular ?
Angular is a Typescript framework used to build dynamic and Single-Page Applications. This has a strong focus on modularity and reusability of code which helps in creating complex and maintainable applications. At the core, Angular has 3 fundamental building blocks, i.e., Service, Directive and Module. These help in shaping the structure, behaviour and functionality of the Application. In this article, we will see each of them in detail, along with understanding their basic implementation through the examples.