2

I have existing website and I got angular2 (cli main.js) on it.

I have got it's main <app></app> tag added for main component.

Now I have another component, which is stats component. it will only show if user is logged in.

The problem is, If I don't add <app-stats></app-stats> inside <app></app> component tag, then it wont render it.

How can I make stats component to be bootstrap without tied to main app component? So It can be put anywhere on the html page without being inside <app></app> and also its optional, if user is not logged in, then it be put inside html code.

  1. Freely add <app-stats></app-stats> on html page
  2. <app-stats></app-stats> (optional - will be only added if user is logged in from server side script "php") - I just want to mention it here, because currently if I dont add this tag, then I get error "missing component...."
Basit
  • 16,316
  • 31
  • 93
  • 154
  • This is an older question, however this method might be useful for you. Found in https://github.com/angular/angular/issues/11730, see https://plnkr.co/edit/pscwfzj6aaih8BbyrTZ5?p=preview for "conditional bootstrapping" of components. https://stackoverflow.com/questions/35418243/sprinkling-angular-2-components-inside-a-non-angular-page may also be of use in answering this – ryanm Jun 01 '17 at 17:00

1 Answers1

1

If you are using Angular RC5, you may bootstrap multiple component and use as suited.

  @Component({
    selector: 'app',
    template: `app`,
  })
  export class App {
    constructor() {}
  }

  @Component({
    selector: 'app-status',
    template: `app-status`,
  })
  export class AppStatus {
   constructor() {}
  }

  @NgModule({
   imports: [ BrowserModule ],
   declarations: [ App ],
   bootstrap: [ App, AppStatus ]
  })
  export class AppModule {}

Here is the Plunker!

Update

you may try below for dynamic addition of components in bootstrap array.

in your app module typescript

let login = require('login.js');

let bootstrapcomponents = [];

if(login.isLoggedIn){
  bootstrapcomponents = [AppComponent, AppStatsComponent]
}
else{
 bootstrapcomponents = [AppComponent]
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: bootstrapcomponents 
})
export class AppModule {}

Now key is when you are doing System.import('<main file which bootstraps your appmodule>') before that login.js file should have been loaded and value must have been set.

You may create a Global variable as well to window object which can have login info set, which may be utilized inside AppModule.

Hope this helps!!

Madhu Ranjan
  • 17,334
  • 7
  • 60
  • 69
  • Yes it does help, actually solves it. Just one more thing. If I wanted to add AppStatus in multiple places and not on one place. what else do I have to do? – Basit Aug 30 '16 at 18:48
  • If I remove ``, Then I get error `The selector "app-stats" did not match any elements` – Basit Aug 30 '16 at 18:50
  • That's not supported, right now. Here is a similar [question](http://stackoverflow.com/questions/39231943/multiple-instances-of-the-same-root-application-in-angular-2). Cheers!! – Madhu Ranjan Aug 30 '16 at 18:50
  • @MaduRanjan do you know how I can overcome the following error? So `app-stats` is optional and not mandatory on the html page. `The selector "app-stats" did not match any elements` – Basit Aug 30 '16 at 19:10
  • if you are bootsrapping the component that needs to be present in the HTML, at least as of now. – Madhu Ranjan Aug 30 '16 at 19:20
  • Is there a way around it, so it can be an optional? – Basit Aug 30 '16 at 20:55
  • where are you checking if the user is logged in or not? If it is outside angular, you may create the bootstrapped component array dynamically based upon if the user is logged in or not, its just a suggestion i dont know what is the actual scenario. – Madhu Ranjan Aug 30 '16 at 21:19
  • I like the idea.. currently on that website I have angular1 with requirejs `require(['app' {% if app.user %}, 'messageNotifications', 'notifications'{% endif %}], function (app) { app.init(); });` like this, so building dynamically bootstrap is good idea I think.. can I just bootstrap on runtime, while there is already bootstrapped before? or in other words, how would I build that array? since everything is `typescript` base and its already compiled from typescript and we would have to do it in javascript. any sample? – Basit Aug 30 '16 at 21:31
  • Bootstrap `main.js` this is what I see after compiled version, so how I can put it dynamic bootstrap, is bit confusing. ` bootstrap: [__WEBPACK_IMPORTED_MODULE_4__app_component__["AppComponent"], __WEBPACK_IMPORTED_MODULE_5__stats_stats_component__["a" /* StatsComponent */]] ` – Basit Aug 30 '16 at 21:40
  • @Basit: updated answer for your scenario, please note i have not tested the code its just an idea how you may resolve it. – Madhu Ranjan Aug 31 '16 at 01:36