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 thesales-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
2 Answers
Reset to default 4Angular 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.
本文标签:
版权声明:本文标题:javascript - Adding new Component element to DOM in Angular 2 at runtime doesn't render the component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744055534a2583184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论