This article is in continuation to our Angular 2 series, if you haven’t had a look at the previous articles, then check-out this link. In this article, we’ll talk about featured module. Now, what is featured module? Let’s first understand, why do we need a featured module? UNtil now, in our application, we are registering our components, Directives, Services directly inside our app.module.ts file but consider that you are working on a real world application. What blunders it will make, if you register tons & tons of files in your root module file. Some issues are given below.
- Difficult to trace an error, if you forgot to register any of your component or Service or Directives.
- No scope for lazy loading.
To avoid such issues, featured module is the answer. Featured modules allow us to group the things together, which are parts of specific functionality. In this way, we can eliminate registering everything at root module & it will also help us to do lazy loading through routing. In our examples, we’ve been registering employee specific components, Services & Directives directly in our root module. It would be great, if we could extract these things from root module & create a featured module for the same, where we can register the things belonging to that specific module & then we can import this module in our root module. To do this, I’m going to create a feature module inside my “emp” folder name “emp.module.ts”.
emp.module.ts File
- import { NgModule } from '@angular/core';
- import { FormsModule } from '@angular/forms';
- import { CommonModule } from '@angular/common';
- import { EmployeeListComponent } from './emp-list.component';
- import { EmployeeSearchPipe } from './emp-search.pipe';
- import { FormatSalaryDirective } from './emp-formatsalary.directive';
- import { EmpService } from './emp.service';
- import { SharedModule } from '../shared/shared.module';
- @NgModule({
- imports: [CommonModule, FormsModule, SharedModule],
- declarations: [EmployeeListComponent, EmployeeSearchPipe, FormatSalaryDirective],
- providers: [EmpService],
- exports: [EmployeeListComponent]//we have to export EmployeeListComponent since we are using it in our app.component.ts file template. If we don't export it will have private access by default i.e. will not be available outside this module
- })
- export class EmpModule {
- }

Ravi KandelPosted Mar 8, 2017, 8:43 AM
Awesome Thanks for sharing
Pankaj Kumar ChoudharyPosted Feb 21, 2017, 9:02 AM
Very useful Stuff Vishal Thanks for Share.........