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.

Similar Reads

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....

Service

...

Directive

A Service is basically a class with a specific purpose. It is a reusable class that can be used by different components in a single application. It is used for sharing data, writing business logic, etc. Services are used to add functionalities like fetching data from the backend, sending data back, and validating the user input. This allows us to follow singleton and not repeat code in multiple components. The tasks which are common in multiple components are written in a service. And it is a good habit to write every business logic in a service. Service uses the Dependency Injection concept to communicate between the components. These are injected into other components or even other services to be used....