8

What are the purposes of these two modules?

import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
ngstschr
  • 2,119
  • 2
  • 22
  • 38
Kevin
  • 795
  • 2
  • 9
  • 21
  • 2
    Possible duplicate of ['angular2/platform/browser' vs. '@angular/platform-browser-dynamic'](http://stackoverflow.com/questions/37993476/angular2-platform-browser-vs-angular-platform-browser-dynamic) – Vogel612 Nov 19 '16 at 17:05

3 Answers3

27

platformBrowserDynamic is a function used to bootstrap an Angular application.

CommonModule is a module that provides all kinds of services and directives one usually wants to use in an Angular2 application like ngIf. CommonModule is platform-independent.

BrowserModule exports CommonModule and provides a few services specific to the browser platform (in contrary to ServerModule or ServiceWorkerModule).

BrowserModule should only be imported in AppModule, CommonModule can be imported everywhere.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 6
    I think ngIf is from CommonModule, BrowserModule just re-exports it. https://angular.io/docs/ts/latest/cookbook/ngmodule-faq.html#!#q-browser-vs-common-module – maxisam Apr 30 '17 at 20:46
  • 3
    Second @maxisam. The main purpose of BrowserModule is different than what presented in this answer. [Angular documentation](https://angular.io/guide/frequent-ngmodules) makes it clear: "For apps that run in the browser, import BrowserModule in the root AppModule because **it provides services that are essential to launch and run a browser app**." and "BrowserModule imports CommonModule, which contributes many common directives such as ngIf and ngFor. **Additionally, BrowserModule re-exports CommonModule** making all of its directives available to any module that imports BrowserModule." – Robert Kusznier Jul 20 '18 at 10:57
  • 2
    Yes, he is right. It wasn't so easy to find out back then when I answered. There were no docs ;-) I updated my answer. – Günter Zöchbauer Jul 20 '18 at 10:59
  • 2
    @GünterZöchbauer Fair point. It was a dark age and you shed some light at that time :) – maxisam Jul 20 '18 at 19:55
8

Angular Modules help organize an application into cohesive blocks of functionality.

Root module needs to import the BrowserModule from @angular/platform-browser to the imports array.

BrowserModule registers critical application service providers. It also includes common directives like NgIf and NgFor which become immediately visible and usable in any of this modules component templates.

QuickStart application is a web application that runs in a browser which involves this Browser Module

PlatformBrowserDynamic - contains the client side code that processes templates

See these links, this might help also: @angular/platform-browser vs. @angular/platform-browser-dynamic

and

https://angular.io/docs/ts/latest/guide/ngmodule.html

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
2

BrowserModule-Exports required infrastructure for all Angular apps. Included by default in all Angular apps created with the CLI new command. Re-exports CommonModule and ApplicationModule, making their exports and providers available to all apps.

platformBrowserDynami-to bootstrap an application.