angular
  1. angular-introduction-to-modules

Angular Introduction to Modules

What are Modules in Angular?

Angular Modules are a way to consolidate different parts of an application into self-contained units of functionality. A module is a collection of services, directives, controllers, filters, and configuration information which are related to a particular area of an application.

Syntax

@NgModule({
  declarations: [ /* List of components, directives and pipes */ ],
  imports: [ /* List of other modules that is to be used */ ],
  providers: [ /* List of dependency injection providers */ ],
  exports: [ /* List of components, directives and pipes to be exported*/]
})
export class AppModule { }

Example

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { EmployeeComponent } from './Employee/employee.component';

@NgModule({
  declarations: [
    AppComponent,
    EmployeeComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Explanation

Angular Module is a class painted with @NgModule() decorator. It is used to decorate a class which needs to be used as a module with the metadata it needs to load the application components.

The declarations property of the NgModule decorator is used to declare the components, directives, and pipes which are defined in our application. The imports property imports other modules. We can define and import any number of modules in our application by adding them to this property. The providers property is used to define the services our application needs, and how they should be created and injected. The exports property is used to export module components, directives, and pipes to other modules when we import this module.

Use

Angular Modules help us to organize our application into reusable code blocks. When working on large applications, we can build each feature of the application as a separate module which can be developed and maintained separately. Modules can have their own configuration, routes, components, directives, pipes, and services.

Important Points

  • Modules helps in organizing the code in a structured way.
  • It is a way of consolidating different aspects of an application into self-contained units of functionality.
  • Modules in Angular are decorated with @NgModule() decorator.
  • The declarations property is used to declare the components, directives, and pipes of our application.
  • The imports property defines and imports all the required modules into our application.
  • The providers array is used to define the services, and how they should be created and injected.
  • The exports array is used to export specific components, directives, and pipes to other modules when we import this module.

Summary

In Angular, Modules provide us a way to consolidate our application into self-contained units of functionality with reusable code blocks. With Modules, our application development becomes more structured and well-organized. The syntax consists of different parts such as declarations, imports, exports, and providers. Understanding how Modules work in Angular is a key to developing maintainable and easily reusable code.

Published on: