Angular 2 innerHTML (click) binding

That’s by design. Angular doesn’t process HTML added by [innerHTML]="..." (except sanitization) in any way. It just passes it to the browser and that’s it.

If you want to add HTML dynamically that contains bindings you need to wrap it in a Angular2 component, then you can add it using for example ViewContainerRef.createComponent()

For a full example see Angular 2 dynamic tabs with user-click chosen components

A less Angulary way would be to inject ElementRef, accessing the added HTML using

elementRef.nativeElement.querySelector('a').addEventListener(...)

Leave a Comment