×

Angular 8 Module

Angular 8 Module

It is a collection of services, directives, controllers, filters, and configuration information. angular.module is used to configure the $injector. The module is a container of the different parts of an application. Controllers always belong to a module. In case of developing a website, the header, footer, left, center, and the right section become part of a module.

Angular Mode

In Angular, a module is a technique to group the components, directives, pipes, and services which are related, in such a way that is combined with other modules to create an application.

Another way to understand Angular modules is classes. In class, we can define public or private methods. The general purposes are the API that other parts of our code can use to interact with it while the individual techniques are implemented details which are hidden.

In the same way, a module export or hide components, directives, pipes, and services. The exported elements are used by other modules, while that are not export in the module and cannot be accessed by other modules of our application.

Angular Module Root

Creating a Module

A module is created by using the Angular function angular.module

Creating Angular module

Use of Module

We have to use the decorator NgModule to define modules.

import {NgModule} from ‘@angular/core’;
 @NgModule({
 Import:[…],
 declarations:[…],
 bootstrap:[…]
 }]
 export class AppModule{} 

In the above example, we turned the class AppModule into an Angular module by using the NgModule decorator. The NgModule decorator needs at least three properties:

  • Imports
  • declarations
  • bootstrap

The property imports expect an array of modules. Where we define the piece of the puzzle. 

The property declaration expects an array of components, directives, and pipes that are the part of the module. 

Angular js Component

The bootstrap property is when we define the root component of the module. However, this property is also an array, 99% of the time, and we are going to explain only one element.

  • Note: There are particular circumstances where more than one component is required to bootstrap a module, but we are not covering those edge cases.

Example:

app/app.component.ts file

import { Component }from ‘@angular/core';
 @Component ({
 selector:'app-root',
 template:
 '<h1>my angular app</h1>'
 }) 

app/app.module.ts file

import { Ngmodule } from ‘@angular/core’;
 import {BrowserModule } from ‘@angular/platform-browser’;
 import {AppComponent} from ‘./app.component’;
 @NgModule({
 imports:[BrowserModule],
 declarations: [AppComponent],
 bootstrap:[AppComponent]
 })
 export class AppModule {} 

app.component.ts is a "hello world” component, nothing is interesting there. In the other hand, the structure that we've seen before for defining a module but in the case, we are defining the modules and components that we are about to using.

The first thing is that we notice our module is importing the BrowserModule like an explicit dependency. The BrowserModule is a built-in module which exports basic pipes, directives, and services. Apart from previous versions of Angular, we have to precisely import those dependencies to use directives like *ngFor or *ngIf in our templates.

The root component of our module is the AppComponent where we have to list it into the bootstrap array.  Because in the declaration property we have supposed to define all elements or pipes that make our application reactive, we have to set the AppComponent again.

Before moving on, there's a necessary clarification to make.

There are two types of modules:

  • Root modules
  • Features modules

We have one root component and many possible secondary components in any application, and only have one root module and zero or more feature modules. To understand bootstrap, we need to know the root module.

Bootstrapping an application in Angular 8

Bootstrapping refers to the starting of a self-dependent process that is supposed to process without external input. To bootstrap our module based application, we need to inform Angular, which is our root module to perform the compilation in the browser. This compilation in the browser is known as "Just in Time" (JIT) compilation process.

main.ts file

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule); 

It is possible to perform the compilation like a build step to our workflow. This method is called "Ahead of Time" (AOT) compilation and will require a slightly different bootstrap process.

NgModule metadata

NgModule is defined through a class decorated with the help of  @NgModule(). The @NgModule() decorator is a function which extracts a single metadata object, whose properties describe the module. The most important features are given below.

  • Declarations: The componentsdirectives, and pipes belong to the  NgModule.
  • Exports: The subset of declarations should be visible and usable into the component templates of other NgModules.
  • Imports: Component templates need the other modules whose exported classes are declared in the NgModule.
  • Providers: Creators of services that NgModule contributes to the global collection of services; they become accessible in all parts of the app. (we can also specify providers at the component, which is selected). 
  • Bootstrap: The main application view, also called the root component, which hosts all the app views. Only the root NgModulewill set the bootstrap property.

src /app/app.module.ts file

import { NgModule } from '@angular/core';
import { BrowserModule }from '@angular/platform-browser';
@NgModule ({
imports:[BrowserModule ],
providers:[Logger ], 
declarations:[ AppComponent ],
exports:[ AppComponent ],
 bootstrap:[ AppComponent ]
})
 export class AppModule{ }  

 App Component is included in the exports list for illustration; it is not necessary in the example. A root NgModule has no any reason to export anything from file because the other modules does'nt import the root NgModule.

NgModules and Components

NgModules provide a compilation context for its components. A root NgModule has a root component which is created during the bootstrap working, but NgModule can include only the additional components. Which load through the router or created by the template.

A component and its template simultaneously define a view. A part contains a view hierarchy, which allows us to represent the complex aimless area of the screen that can be created, modified, and destroyed as a unit. A view hierarchy can define in components which belong to different NgModules.

NgModules and Components

 When we create a component, it is connected directly with a single view, called the host view. The host view is the root of a view hierarchy, which contains embedded views, which are, in turn, the host view of other components. Those components can in the same NgModule and be imported from other NgModules. Views in the tree can nested to any depth.

  • Note: The hierarchical structure of views is a key factor in the way Angular detects and responds to change in the DOM and app data.

NgModules and JavaScript modules

The Ngmodule system is different from the JavaScript module system for managing collections of JavaScript objects. These are complementary module systems that we can use together to write our app.

In JavaScript, every file is a module and object defined in the file belong to that module. The module. The module declares some objects by marking them with the export keyword. Other JavaScript module use import statement to access public objects from other modules.

import { NgModule }     from '@angular/core';
import { AppComponent } from './app.component';
export class AppModule { } 

Difference between Module and Component

Module Component
A module is a collection of components, services, pipes, directives, and so on. A component in Angular is a building block of the application with an associated template view.
The angular apps will contain many modules, each dedicated to a single purpose. Each component can use another component, which is declared in the same module. To use components declared to other modules, they need to export from this module.
Denoted by @ngModule Denoted by @component

Related Topics

Advantages | Disadvantages of Angular 8

Advantages of Angular 8 There are some Advantages of angular 8 which are given below: It offers clean code developmentHigher PerformanceAn angular framework can take care of routing, which...

1 minute read.

Angular 8 Conclusion

Angular version 8 looks like a more accessible solution entirely focused on the modern technological trends added features. Like Ivy, route configurations use dynamic imports, builder APIs in the CLI, web worker support,...

2 minutes read.

Angular 8 File Structure

File Structure in Angular 8: A file structure contains the files for one or more projects. A project is the set of files that comprise an application on a shareable...

3 minutes read.

Installation of Angular 8

Installation Guides to Angular 8 step by Step Angular combines declarative templates, dependency injection and, end to end tooling to solve development challenges. Angular empowers developers to build an application that lives on the...

4 minutes read.

Angular 8 ngIf Directive

ngIf Directive is the part of structural directives.The NgIf is the most straightforward structural Directive and comfortable to understand. The ngIf Directives is used to add and remove HTML elements according to...

2 minutes read.

Angular 8 Tutorial

What is Angular 8 Angular is a Framework of JavaScript used to build web and mobile applications. Angular 8 is a client-side TypeScript based structure which is used to create dynamic...

6 minutes read.

Creating form in Angular 8

The angular reactive form is used to handle the user's input. We can use Angular form in the application to authorize the user to log in, to update profile, to enter information,...

2 minutes read.

Dynamic components in Angular 8

Dynamic components in Angular 8 The dynamic component is one of the versatile and core concept introduced in Angular, Component template is not fixed. An application needs to load new elements...

4 minutes read.

Difference between Angular and React

Difference between Angular and React Parameters AngularReactType Angular is a complete framework. React is a JavaScript library, and much older compared...

2 minutes read.

Angular 8 Components

Angular is used for building mobile and desktop web applications. The component is the basic building block of Angular. It has a selector, template, style, and other properties, and it...

4 minutes read.

Angular 8 Module

Angular 8 Module It is a collection of services, directives, controllers, filters, and configuration information. angular.module is used to configure the $injector. The module is a container of the different parts of an application....

6 minutes read.

Angular CLI Commands

All CLI commands of Angular Angular CLI is a command-line interface which is used to initialize, develop, and maintain Angular applications. We can use these Command on command prompt or consequentially by an associated...

8 minutes read.

Angular 8 Animation

What is Animation? The animation is a process of drawing, designing, making layouts, and preparation of photographic sequences which are integrated into the multimedia and gaming products. A reflection of movement created for displaying...

5 minutes read.

Angular 8 Architecture

Architecture of Angular 8 Angular 8 is a platform and framework which is used to create clients applications in HTML and Typescript. Angular 8 is written in Typescript. Typescript is a...

5 minutes read.

Dependency Injection in Angular 8

Dependency injection (DI), is an essential application design pattern. Angular 8 has its own DI framework, which used in the design of Angular application to increase efficiency and portability. Dependencies are the services that...

7 minutes read.

Angular 8 universal

Angular 8 universal Angular Universal is a technology that allows server-side rendering for Angular apps. An angular app is a single-page app, and it runs in a client-side browser. Rendering pages in the DOM is...

6 minutes read.

Angular 8 App Loading

How an Angular 8 app loaded and started When we create an Angular app and run it by using ng serve command, it looks like the below screenshot. It is a simple...

3 minutes read.

$timeout service in AngularJS

The discipline of web development is expanding quickly. A technology that is released today will inevitably become obsolete in a few months. The webpages were static in the past and...

4 minutes read.

Angular 8 changes and new features

The angular version number indicates the level of changes introduced by the release. The use of semantic versioning helps us understand the potential impact of updating to a new version. Angular is the...

5 minutes read.

Event Binding in Angular 8

Event binding in Angular 8 Event binding is a data-binding from an element to a component. User actions such as keystrokes, mouse movement, clicks, and touches can be bound to the component property...

1 minute read.