Defining Providers in Shared Modules in Angular

Published on August 14, 2017


Defining Providers in Shared Modules in Angular

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Sometimes you’ll want to create a shared module in an Angular app that defines services, pipes and directives that feature modules and lazy-loaded modules can use. The one little hiccup is that services, which are normally supposed to act as singletons, could end up being provided multiple times, especially for lazy-loaded modules. Luckily for us however, there’s an easy fix for that specific use-case by defining a static forRoot method in the shared module that returns a ModuleWithProviders object.

Here’s a sample implementation. First, our shared module:

import { NgModule, ModuleWithProviders } from '@angular/core';

import { MyDirective } from './my.directive';
import { FunPipe } from './fun.pipe';
import { SomeService } from './some.service';

Notice how we declare and export our pipes and directives as usual in the NgModule’s metadata, but we don’t provide the service. Instead, we define a static forRoot method in the module’s class that returns an object that implements Angular’s ModuleWithProviders interface.

Now, in our app module, we can import the shared module and call forRoot on it to also provide our service:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { SharedModule } from './shared/shared.module';

You’ll probably notice that you’ve seen this in action already when importing the RouterModule and calling forRoot on it in your app module.

Then finally, in any feature module we can simply import the shared module without the forRoot and we’ll have access to the shared pipes and directives without providing the service again:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { SharedModule } from '../shared/shared.module';
// ...

🍕 That’s it! A neat little trick to make it easier to work with shared modules and lazy-loaded feature modules.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Needs examples. How do I use forRoot()?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel