I am trying to call the child component menthod from the parent ts file but i am unable to do call. getting error Cannot read property 'doSomething' of undefined.
export class AddComponent implements OnInit, OnDestroy {
public doSomething()
{
alert("Called");
}
}
Parent code :
@ViewChild(AddComponent) child:AddComponent;
ngAfterViewInit() {
this.child.doSomething();
}
parent html file
<span class="glyphicon glyphicon-plus addBack" title="Add new booking" (click)=" openActionModal(GridActions, $event, null, 'add')"
style="float: right;margin-right: 10px;margin-top: 18px;"></span>
<ng-template #GridActions>
<div class="modal-header">
<button type="button" class="close pull-right" aria-label="Close" (click)="openConfirmationModal(EditConfirmation)">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<app-add #child *ngIf="showModalAdd"> </app-add>
</div>
</ng-template>