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

3
  • 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 Commented May 20, 2016 at 9:54
  • the problem is that I want to add elements dynamically .. it's not about repeating elements Commented May 20, 2016 at 9: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) Commented May 20, 2016 at 9:57

2 Answers 2

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

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

Comments

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.

5 Comments

Can you give an example on how to use that in this case?
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 ;)
I want to do execute this into a separate js file which is not related to AngularJS/.Typescript. Is it possible?
yeah .. something like this.
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 :) )

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.