简体   繁体   中英

How to load Angular2 root component Dynamically

I have a requirement where I have to load the root component in angular2 dynamically/using setTimeout(). Is is possible and how. eg

<body>
 <hello-world>Loading...</hello-world>
</body>

I want the ' hello-world ' append after few second of page load. As I can't have custom component in the page load.

A plnkr will be helpful.

You can add event on index page and listen this event in main.ts angular file.

index.html:

...  
<body>
    <my-app>Loading...</my-app>

    <script>
      setTimeout(function() {
          document.dispatchEvent(new Event('startNgLoad'))
      }, 5000)
    </script>
  </body>
...

main.ts:

document.addEventListener('startNgLoad', () => {
  platform.bootstrapModule(AppModule);
})

You can load the root module inside the setTimeout in main.ts

setTimeout(function() {
  platformBrowserDynamic().bootstrapModule(AppModule);
},1000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM