2

I'm trying to add dynamically (at runtime) new nodes in the DOM. The new nodes basically are the Component selectors. The problem is that the content of the component is not rendered so the result is just having empty HTML tags.

I'm using Angular 2.0 Documentation example and modified it a little bit just to show case.

Here is the plunker

All I do is to reuse the code and when I click on the body to create new sales-tax elements. Obviously it's not working.

This is the new script element I added ontop of body.

<script>
  function add(){
     var element = document.createElement('sales-tax');
    document.getElementsByTagName("my-app")[0].appendChild(element);
  }
</script>

and the function is invoked once I click the body.

 <body onclick="add()">
    <my-app>Loading...</my-app>
 </body>

This is the result. As you can see the sales-tax that has been added to the template pre runtime it's rendered but the ones that I add at runtime are empty.

This is the result

Tek
  • 1,178
  • 1
  • 12
  • 22
  • I would go a different route. Since you're having several `sales-tax` elements side by side, why not make it a list and you just add new SalesTax "instances" to it? Where does the `sales-tax` belong to? Or are you really just testing things out on how to repeat elements? See [*ngFor](https://angular.io/docs/ts/latest/api/common/NgFor-directive.html) – Maximilian Riegler May 20 '16 at 09:54
  • the problem is that I want to add elements dynamically .. it's not about repeating elements – Tek May 20 '16 at 09:55
  • You still can, just make yourself a button wherever you want and leverage injectable services to hold your data. (granted you're doing that from inside an Angular component) – Maximilian Riegler May 20 '16 at 09:57

2 Answers2

4
  • Angular renders the dom interanlly to generate actual DOM, and binds them using Zone.js, which is only one-way, changes made to DOM are not detected.

  • Angular only detects a change if it's ran inside Zone.js's scope.

  • Then there are other interanl bindings and generation procedures to make a component actually work and bind with rest of the app.

  • To Achieve what you're trying do, you'll have to use DynamicComponetLoader as @echonax mentioned, but since it's deprecated now, you should use ViewContainerRef.createComponent() instead.

For implementaion see THIS Answer or @Gunter's PLUNKER (hope he doesn't mind.)

cbedrosian
  • 460
  • 5
  • 11
Ankit Singh
  • 24,525
  • 11
  • 66
  • 89
2

createComponent() version: Plunker

this.resolver.resolveComponent(HeroListComponent).then((factory:ComponentFactory<any>) => {
      this.cmpRef = this.theBody.createComponent(factory)
    });

beta.17 (deprecated) version with .loadNextToLocation(): Plunker(deprecated)

 addCmp(){
    console.log('adding');
    this.dcl.loadNextToLocation(HeroListComponent, this.theBody);
  }

You are not creating a component you are just creating an html element and appending it to the DOM which happens to have the same name with a components selector.

In order to achieve what you want you have to use DynamicComponentLoader.

eko
  • 39,722
  • 10
  • 72
  • 98
  • Can you give an example on how to use that in this case? – Maximilian Riegler May 20 '16 at 09:49
  • 1
    Well it's definitely a step to what I'm trying to do ... I just have to tweak it to my needs. Thank you echonax ;) – Tek May 20 '16 at 10:00
  • I want to do execute this into a separate js file which is not related to AngularJS/.Typescript. Is it possible? – Tek May 20 '16 at 10:04
  • yeah .. something like this. – Tek May 20 '16 at 10:43
  • Well naaah I'm not creating a new framework but in my current work I have to inject different components based on different environments ..so I want to have one environment containing one component and in the other env I want to have same component plus another one .. (Can Ekin Çam :) ) – Tek May 20 '16 at 10:55