I have two components child and parent components each having its own directory.
child folder
child.component.html
<p>{{message}}</p>
<button (click)="sendMessage()">SendMessage</button>
child.component.ts
import {Component, Input, OnInit, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
@Input('msg') message: string;
msg = 'I am child';
@Output('smEvent') sendMessageEvent = new EventEmitter<string>();
constructor() { }
ngOnInit(): void {
}
sendMessage() {
this.sendMessageEvent.emit(this.msg);
}
}
parent folder parent.component.html
<app-child [msg]="testMessage" (smEvent)="sendMessageEvent($event)"></app-child>
<p>{{msgs}}</p>
parent.component.ts
import {Component, OnInit, Output} from '@angular/core';
@Component({
selector: 'app-parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
testMessage = 'test';
msgs = '';
constructor() { }
ngOnInit(): void {
}
sendMessageEvent($event) {
console.log(' child => ' + $event.msg); //returns undefined
this.msgs = $event.msg;
}
}
app.component.html
<app-parent></app-parent>
I am able to do parent to child communication but in case of the child to parent communication, I am not able to do so. How can I able to communicate and what did I miss?