Sourav Tomar
Sourav Tomar

Reputation: 23

How to call a child component menthod from parent if the child component is not being called yet?

I have a parent component and a child component to add and update tile using a popum component, now add button happen to be in parent component and update is in child component. when there is no tiles(child component markup) to show, i have to add a tile that time i'm not able to call the child component's method openAddOrUpdatePopup().

How can i call this method in parent component without being depending on child component ?

My code: - parent-html

<input type="button" class="btn" value="Add" (click)="applicationTile.openAddOrUpdatePopup()" />
<application-tile class="grid-item action-grid-item enable-events" *ngFor="let application of paginationParams.data" [ngClass]="{'active':activeApplication && activeApplication.applicationId == application.applicationId}" [application]="application"></application-tile>

parent.ts

export class ApplicationsComponent implements OnInit {
 @ViewChild (ApplicationTileComponent, {static: false}) applicationTile: ApplicationTileComponent ; 
}

Child.html

<label class="name">{{application.applicationName}}</label>
<div class="table-view">

<div class="table-view-row">
    <div>
        <label>Application value </label>
        <span>{{application.value ? application.value  : '-'}}</span>
    </div>
        <label>Application Version</label>
        <span>{{application.appVersion ? application.appVersion : '-'}}</span>
    </div>
    <ul class="actions">
        <li>
            <span class="icon svg-icon action-icon edit-icon" title="Edit" 
 (click)="openAddOrUpdatePopup(application, $event)"></span>
        </li>

    </ul>
</div>

<av-popup class="medium-popup" *ngIf="popupParams && popupParams.name == 'create-or-update-application'" [popupParams]="popupParams" (close)="popupParams = null">
<form name="form" (ngSubmit)="f.form.valid && popupParams.saveOrUpdateApplication()" #f="ngForm"></form></av-popup>

child.ts

 openAddOrUpdatePopup(application?, $event?) {
       if (application) {
        application = JSON.parse(JSON.stringify(application));
       }
       else {
        application = {
        applicationType: this.applicationTypes[0].applicationTypeVal
       }
     }

this.popupParams = {
  name: 'create-or-update-application',
  title: application.applicationId ? 'Edit ' + application.applicationName : "Create New Application",
  application: application
}

}

Upvotes: 0

Views: 78

Answers (1)

Hemanth
Hemanth

Reputation: 1

Can you elaborate the question bit more? OR can you post the working code in Stackblitz and provide the link?

There are few ways to connect with Parent & child component by using,

Input & Output.

Shared service with Observer (Publish & subscribe)

Upvotes: 0

Related Questions