In my child component i have two buttons like following:
child-component.html
<div>
<button (click)="getFirstData()">First</button>
<button (click)="getLastData()" >Last</button>
</div>
In child-component.ts:
export class ChildComponent implements OnInit, AfterViewInit {
firstItem: number;
lastItem: number;
constructor() {
}
ngOnInit() {
}
ngAfterViewInit(): void {
}
getFirstData() {
this.firstItem = 1;
return this.firstItem;
}
getLastData() {
this.lastItem= 10;
return this.lastItem;
}
}
In parent component i need to receive the firstItem
and lastItem
data after the respective method is invoked from child component. When i try to get that in parent component using ViewChild
in AfterViewInit
lifecycle hook like following:
parent-component.ts
export class ParentComponent implements OnInit, AfterViewInit {
@ViewChild(ChildComponent, {static: true}) child: ChildComponent;
first: number;
last: number;
ngOnInit() {
}
ngAfterViewInit() {
console.log('first value ', this.child.getFirstData());
console.log('last value ', this.child.getLastData());
}
getValues(): void {
// some operations heer
}
}
I can't get that data from child.
My goal is to receive those data after method invoked in child after button click event there. And after receiving, i need to call the getValues()
method in parent component.
How can i achieve this ?