admin管理员组

文章数量:1357312

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

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

Share Improve this question edited May 20, 2016 at 9:45 Tek asked May 20, 2016 at 9:37 TekTek 1,1982 gold badges12 silver badges23 bronze badges 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 – Maximilian Riegler Commented May 20, 2016 at 9:54
  • the problem is that I want to add elements dynamically .. it's not about repeating elements – Tek 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 ponent) – Maximilian Riegler Commented May 20, 2016 at 9:57
Add a ment  | 

2 Answers 2

Reset to default 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 ponent 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.)

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 ponent you are just creating an html element and appending it to the DOM which happens to have the same name with a ponents selector.

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

本文标签: