admin管理员组文章数量:1291834
I'm dynamically creating ponents inside appponent.ts
using the ViewContainerRef.createComponent()
method, which returns a ComponentRef
object.
let newComponent:ComponentRef<any> = this.filtersSection.createComponent(MyDateRangeComponent);
I need to dynamically add an event listener to this ponent so it listens to the onDateRangeChange
event and executes the dateRangeChanged(event)
method defined inside the appponent.ts
ponent.
I originally used this ponent this way inside the appponent.html:
<my-daterange (onDateRangeChange)="dateRangeChanged($event)"></my-daterange>
I've found that this could be achieved using the Renderer
class but I couldn't make this working:
this.renderer.listen(newComponent, 'click', (event) => {
// Do something with 'event'
console.log(event);
});
Any help with this would be really appreciated.
I'm dynamically creating ponents inside app.ponent.ts
using the ViewContainerRef.createComponent()
method, which returns a ComponentRef
object.
let newComponent:ComponentRef<any> = this.filtersSection.createComponent(MyDateRangeComponent);
I need to dynamically add an event listener to this ponent so it listens to the onDateRangeChange
event and executes the dateRangeChanged(event)
method defined inside the app.ponent.ts
ponent.
I originally used this ponent this way inside the app.ponent.html:
<my-daterange (onDateRangeChange)="dateRangeChanged($event)"></my-daterange>
I've found that this could be achieved using the Renderer
class but I couldn't make this working:
this.renderer.listen(newComponent, 'click', (event) => {
// Do something with 'event'
console.log(event);
});
Any help with this would be really appreciated.
Share Improve this question edited Oct 14, 2016 at 15:38 Günter Zöchbauer 658k233 gold badges2.1k silver badges1.6k bronze badges asked Oct 14, 2016 at 15:30 faguilera85faguilera85 1451 gold badge4 silver badges11 bronze badges1 Answer
Reset to default 12Using ComponentRef.instance
allows you to access the ponent instance and with this you can subscribe to the EventEmitter
:
newComponent.instance.onDataRateChange.subscribe(evt => this.result = evt);
本文标签: javascriptAdd event binding to dynamically created componentsStack Overflow
版权声明:本文标题:javascript - Add event binding to dynamically created components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741539665a2384244.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论