2

Is there any way to inject my Angular 4 component (widget-like mail window app using webpack) into any static website, preferably using the <script> tag?

Found some blog posts but all of them are using SystemJS and my app is using webpack and there seem to be no results with that.

2 Answers 2

3

Angular app must always have root element, which is usually inserted in index.html's <body>. Assuming you have written your widget, in which root component f.e. is <widget>, you can mix it up with any static markup you have on your page:

<!doctype html>
<html>

  <head>
    <title>title</title>
  </head>

  <body>

    <header>
        <nav>Brand</nav>
    </header>

    <aside>
        Menu
        <!-- It can also be injected in any other element -->
        <widget></widget>
    </aside>

    <main>
        Content
    </main>

  </body>

</html>

If your app is bootstrapped with Angular CLI, which under the hood uses webpack, just put your static markup in src/index.html and you are done.

Edited: if you want to inject your widget into any other page, you can try to do this with following steps:

  1. Create one more script, in which create your app's root element and then paste it into the body
  2. Bundle your Angular app into static files. Better to have one .js file for simplicity.
  3. Dynamically create script element and supply static bundle's path to script's src
  4. Dynamically inject it into the page's body

Kind of like that:

<script type="text/javascript" charset="utf-8">
    window.addEventListener('load', () => {
      const widgetElement = document.createElement('widget')
      // document.body is not necessary here, it can be any other element
      document.body.appendChild(widgetElement) 

      const widgetCode = document.createElement('script')
      widgetCode.src = '<path to bundled angular app>'
      document.body.appendChild(widgetCode)
    })
</script>

I've created plnkr, which may help you a bit

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer :) My question doesn't concern putting the components in my own static page, but what to do / what scripts to add / where to host the app and how to inject the application code in someone else' website. So that someone could just add a script to his static website and that would add my component to that website.
It sounds like you are trying to create a library?
My task was to (given a working backend) create an angular widget that would display messages from that backend etc (like a simplified mail client) that could be appended to any page preferably via the script tag (this is the part that I'm unclear about).
Ok, having tried a couple more things appears it's sufficient to dynamically add to the head element script tags with all files bundled with ng build :) Edit: which is exactly what vanelizarov suggested, thank you for the answer
3

you can use custom elements in angular to creat a custom component and then you build your projetc with npm run build element, it will generate a script .js for you where all your applications is built into. And then you can inject the script into any other project or a simple html page and it will load youre application. May be my explanation is not very clear you can see cutom elements in angular main website.

Comments

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.