In the CLI generated feature module, there are two JavaScript import statements at the top of the file
- NgModule: InOrder to use the
@NgModuledecorator - CommonModule: It provides many common directives such as
ngIfandngFor.
In the CLI generated feature module, there are two JavaScript import statements at the top of the file
@NgModule decoratorngIf and ngFor.Below are the main differences between Angular NgModule and javascript module,
| NgModule | JavaScript module |
|---|---|
| NgModule bounds declarable classes only | There is no restriction classes |
| List the module's classes in declarations array only | Can define all member classes in one giant file |
| It only export the declarable classes it owns or imports from other modules | It can export any classes |
| Extend the entire application with services by adding providers to provides array | Can't extend the application with services |
There are two common possible errors with declarations array,
Below are the steps to be followed to use declaration elements.
If you do import BrowserModule into a lazy loaded feature module, Angular returns an error telling you to use CommonModule instead. Because BrowserModule’s providers are for the entire app so it should only be in the root module, not in feature module. Whereas Feature modules only need the common directives in CommonModule.

Question 231
Below are the five categories of feature modules,
Question 232
A provider is an instruction to the Dependency Injection system on how to obtain a value for a dependency(aka services created). The service can be provided using Angular CLI as below,
ng generate service my-service
The created service by CLI would be as below,
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root', //Angular provide the service in root injector
})
export class MyService {
}
You should always provide your service in the root injector unless there is a case where you want the service to be available only if you import a particular @NgModule.
It is possible to restrict service provider scope to a specific module instead making available to entire application. There are two possible ways to do it.
import { Injectable } from '@angular/core';
import { SomeModule } from './some.module';
@Injectable({
providedIn: SomeModule,
})
export class SomeService {
}
import { NgModule } from '@angular/core';
import { SomeService } from './some.service';
@NgModule({
providers: [SomeService],
})
export class SomeModule {
}
Question 235
There are two possible ways to provide a singleton service.
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class MyService {
}
@NgModule({
...
providers: [MyService],
...
})
If a module defines provides and declarations then loading the module in multiple feature modules will duplicate the registration of the service. Below are the different ways to prevent this duplicate behavior.
If the RouterModule module didn’t have forRoot() static method then each feature module would instantiate a new Router instance, which leads to broken application due to duplicate instances. After using forRoot() method, the root application module imports RouterModule.forRoot(...) and gets a Router, and all feature modules import RouterModule.forChild(...) which does not instantiate another Router.
Question 238
The Shared Module is the module in which you put commonly used directives, pipes, and components into one module that is shared(import it) throughout the application.
For example, the below shared module imports CommonModule, FormsModule for common directives and components, pipes and directives based on the need,
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { UserComponent } from './user.component';
import { NewUserDirective } from './new-user.directive';
import { OrdersPipe } from './orders.pipe';
@NgModule({
imports: [ CommonModule ],
declarations: [ UserComponent, NewUserDirective, OrdersPipe ],
exports: [ UserComponent, NewUserDirective, OrdersPipe,
CommonModule, FormsModule ]
})
export class SharedModule { }
Question 239
No, it is not recommended to share services by importing module. i.e Import modules when you want to use directives, pipes, and components only. The best approach to get a hold of shared services is through 'Angular dependency injection' because importing a module will result in a new service instance.
In Angular 9.1, the API method getLocaleDirection can be used to get the current direction in your app. This method is useful to support Right to Left locales for your Internationalization based applications.
import { getLocaleDirection, registerLocaleData } from '@angular/common';
import { LOCALE_ID } from '@angular/core';
import localeAr from '@angular/common/locales/ar';
...
constructor(@Inject(LOCALE_ID) locale) {
const directionForLocale = getLocaleDirection(locale); // Returns 'rtl' or 'ltr' based on the current locale
registerLocaleData(localeAr, 'ar-ae');
const direction = getLocaleDirection('ar-ae'); // Returns 'rtl'
// Current direction is used to provide conditional logic here
}