0

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">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      <app-add #child *ngIf="showModalAdd"> </app-add>
    </div>

  </ng-template>
Vishal
  • 139
  • 1
  • 5
  • 12

3 Answers3

0

You can do this by using @ViewChild

@Component({
 selector: 'child-cmp',
 template: '<p>child</p>'
})
class ChildCmp {
   doSomething() {}
 }

@Component({
  selector: 'some-cmp',
  template: '<child-cmp></child-cmp>',
 directives: [ChildCmp]
})
class SomeCmp {
 @ViewChild(ChildCmp) child:ChildCmp;
 ngAfterViewInit() {
    // child is set
    this.child.doSomething();
  }
}

Using string selector

@Component({
 selector: 'child-cmp',
 template: '<p>child</p>'
 })
class ChildCmp {
   doSomething() {}
}
@Component({
    selector: 'some-cmp',
    template: '<child-cmp #child></child-cmp>',
    directives: [ChildCmp]
})
class SomeCmp {
    @ViewChild('child') child:ChildCmp;
    ngAfterViewInit() {
    // child is set
    this.child.doSomething();
  }
} 

Hope this will help

Sneha Pawar
  • 1,097
  • 6
  • 14
0

I believe you forgot to add child component selector in your parent template file.

parent.component.html

<add-component></add-component>

parent.component.ts

@ViewChild(AddComponent) child:AddComponent;
   ngAfterViewInit() {
   this.child.doSomething();
}

add.component.ts

export class AddComponent implements OnInit, OnDestroy {
    public doSomething()
    {
       alert("Called");
    }
 }

Hope this will solve your problem!

TheParam
  • 10,113
  • 4
  • 40
  • 51
  • I am calling the child view in the bootstrap modal popup i added it but it is not working. – Vishal Jan 30 '19 at 09:19
  • declare reference variable in template modal tag and @ViewChild('referencevarname') child:AddComponent; add here – TheParam Jan 30 '19 at 09:24
  • I have declared it but still it is not working please check I have added the html file code in the question – Vishal Jan 30 '19 at 09:49
-1

Did you make the reference in the HTML/template? You need to make Template Reference to call the ViewChild-

Let, your AddComponent's selector is add-component. So call this like below in HTML/template-

<add-component #child></add-component>
Shofol
  • 693
  • 1
  • 11
  • 26
  • OP is calling like `@ViewChild(AddComponent) ` so no need to define a template variable such as `#child`. It is needed when calling like `@ViewChild('child', {read: AddComponent})` – Harun Yilmaz Jan 30 '19 at 08:57