0

parent-component.component.ts

@Component({
  selector: 'app-parent-component',
  templateUrl: './parent-component.component.html',
})
export class ParentComponentComponent {
  someFunction() {
    childComponent.someChildFunction()
  }
}

parent-component.component.html

<ngTemplate>
  <app-child-component></app-child-component>
</ngTemplate>

child-component.component.ts

@Component({
  selector: 'app-child-component',
  templateUrl: './child-component.component.html',
})
export class ChildComponentComponent {
  someChildFunction() {
    //some usefull work
  }
}

child-component.component.html

<input [(ngmodel)]="search">

How to call the child function from the parent that is within the ngTemplate

i tried to it using the component reference.

@ViewChild(ChildComponentComponent)
  private childComponent: ChildComponentComponent;

but was undefined.. then tried

<ng-container *ngTemplateOutlet="template"></ng-container>

in parent component

That time any update inside the child component is not reflecting like an input with ngModel.

Any help would be helpful. Or some better solution would also work..

  • [refer this it will help you](https://stackoverflow.com/questions/38974896/call-child-component-method-from-parent-class-angular) You need to use @viewChild From that you can get child from parent – Sangar Lal Nov 20 '20 at 05:12
  • Why is the child component wrapped inside ``? – Nicholas K Nov 20 '20 at 05:28
  • Actually I am using a ngx modal.. hence the component is wrapped in the ngTemplate.. Is it not the correct way? – Robin Joseph Nov 21 '20 at 02:59
  • Actually i can explain the full thing.. may be you will get a better idea.. I am using ngx bootstrap modal and the modal content are refered inside the ngTemplate. Now the content inside modal was reused in many place so i created a component out of it. basically its a form that gets in details. now the form submit button is in modal level and the save part is inside the reused component. when i hit the submit button i want to hit the save part of the reused component. Any better solution should be helpful – Robin Joseph Nov 21 '20 at 03:06

0 Answers0