I would like the child component to send some data to the parent component. The child part is executed as expected, but the parent code doesn't react, and there are no errors. So if have any idea ...
Child HTML
<button (click)="sendDataToParent()"> CLICK ME ! </button>
Child TS
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Hero } from '../Hero';
@Component({
selector: 'app-hero-detail',
templateUrl: './hero-detail.component.html',
styleUrls: ['./hero-detail.component.css']
})
export class HeroDetailComponent implements OnInit {
@Input() hero : Hero;
@Output() sendDataHeroEvent: EventEmitter<Hero> = new EventEmitter<Hero>();
constructor() { }
ngOnInit() { }
sendDataToParent(event: any){
let hero2: Hero; hero2 = new Hero(); hero2.id = 12; hero2.name = "Narco";
this.sendDataHeroEvent.emit(hero2);
console.log("OK, child emits data to parent");
}
}
Parent HTMl
<h2>My Heroes</h2>
<ul class="heroes"
(sendDataHeroEvent)="sendDataHeroEventHandler($event)">
<li *ngFor="let hero of heroes"
(click)="onSelect(hero)"
[class.selected]="hero === selectedHero"
class="drag-box"
cdkDragLockAxis="y"
cdkDrag>
<span class="badge">{{hero.id}}</span> {{hero.name}}
</li>
</ul>
<app-hero-detail [hero]="selectedHero"></app-hero-detail>
Parent TS
import { Component, OnInit } from '@angular/core';
...
export class HeroesComponent implements OnInit {
...
sendDataHeroEventHandler(event){
console.log("OK, parent handler is called");
}
In the console log of chrome, i get "OK, child emits data to parent" but i do not get "OK, parent handler is called"