Passing a function as parameter from parent to child component has resulted in the trigger of the parent component's function when a click event occurs. However, all properties of the parent component are undefined in this scenario. For instance,
Parent Component
export class AppComponent implements OnInit {
constructor( private notificationService: NotificationService ) {}
unreadNotification(): Observable<any> {
// here this.notificationService is undefined
console.log( this.notificationService );
}
}
Parent html
<notification-menu [unread]= "unreadNotification"></notification-menu>
child Component
export class NotificationMenuComponent implements OnInit {
@Input() updateUnread: Function;
}
child html
<button type="button" class="icon-button" (click)="updateUnread()">
</button>
When clicking on the notification button, unreadNotification
is triggered, but the value of this.notificationService
in the console.log
remains undefined
.
How can this issue be resolved?