0

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

<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.

2 Answers 2

1

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);
})
Sign up to request clarification or add additional context in comments.

2 Comments

I cant have <my-app> tag in the beginning of index.html load. Can I append it dynamically and later do 'startNgLoad' ?
Yep, place document.body.appendChild(document.createElement("my-app")) before dispatch event, or before platform.bootstrapModule()
0

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

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

1 Comment

Is it possible setTimeout => insert('<hello-world>') after 5 seconds of set timeout

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.