I am displaying a button from child component to AppComponent(parent). Whenever the button is clicked I would like to invoke the 'showAlert()' method if 'lastPage' value is set to true. But it doesn't seem to work. Attached a stackblitz example
Is this a correct way to invoke a function from the child component? is there a different way to do it?
app.component.html
<app-child [lastPage]="lastpage"></app-child>
app.component.ts
export class AppComponent {
lastpage = true;
name = 'Angular ' + VERSION.major;
}
child.component.html
<button>Click me for Alert</button>
child.component.ts
export class ChildComponent implements OnInit {
@Input() lastPage?: boolean
constructor() { }
ngOnInit() {
this.showAlert()
}
showAlert() {
if (this.lastPage) {
alert('Button Clicked from child');
}
}
}