I am going through this tutorial to understand angular 2 ng-content. I want to capture an event that fires on ng-content. I have the following component:
ng-content
@Component({ moduleId: module.id, selector: 'card', template: ` <ng-content (click)="onClick($event)"></ng-content> ` }) export class CardComponent { onClick(){ console.log('clicked'); } }
Here, as you can see, I set the listener to the event click. But for some reason this does not work. It seems that the whole tag is ng-contentreplaced. Therefore, to capture this event, I do this:
click
template: ` <div (click)="onClick($event)"> <ng-content></ng-content> </div> `
Is there a better way to do this? because I donโt want to wrap my ng-contentin divto avoid design problems. Thank you Plnkr
div
<ng-content> , . Angular .
<ng-content>
DIV - ng-content.
DIV
, , <ng-content>. AppContentComponent (-),
AppContentComponent
<app-content (click)="onClick()"> <ng-content></ng-content> </app-content>
, , ( <div>, <app-content>), , , <ng-content>, , , .
<div>
<app-content>
Source: https://habr.com/ru/post/1015401/More articles:Git adding to a modified file does not work, except with -p (patch) - gitHow to remove a Google project from the Firebase Console? - firebaseAndroid listview extra space at the end when scrolling - androidHow to get two related objects in Laravel (Eloquent) with one SQL query - eloquentHow to get value between two different tags using a beautiful soup? - pythonmultiple "md-tabs" have the "md-active" class at the same time, when I often switch tabs using angular md-selected index - javascriptCan I do a search through Git Stash? - gitStatic const data element defined in another file - c ++angular 2 ngAnvis error analysis model - angularerror C2106: '=': the left operand must be l-value - c ++All Articles