I would like to bootstrap an Angular 4 micro app multiple times on the same page. Basically, foreach instance of class ‘.angular-micro-app’, bootstrap a new app instance.
I understand that traditionally these would be Angular components within a single parent App. In my case, that is not possible and I need multiple instances of the same root level app (component), on the same page. This was fairly trivial with AngularJS 1.x, but is proving rather frustrating with Angular.
Example:
index.html snippet:
<body>
<div class=“.angular-micro-app”></div>
…
<div class=“.angular-micro-app”></div> <!-- this element is NOT bootstrapping -->
</body>
app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: '.angular-micro-app',
template: require('./app.component.html'),
})
export class AppComponent { }
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
],
providers: [
],
bootstrap: [AppComponent],
})
export class AppModule { }
In a main file, I’m doing the basic platform bootstrap:
platformBrowserDynamic().bootstrapModule(AppModule);