H
H
hobu4ok912016-12-03 07:56:03
Angular
hobu4ok91, 2016-12-03 07:56:03

How to call component method in ng-content in Angular 2?

The title is not entirely clear, here is a specific example:
HomeComponent template:

<app-step-controls>
  <button>Назад</button>
  <button (click)="toNextStep('test')">Вперед</button>
</app-step-controls>

AppStepControlsComponent template:
<div>
  <ng-content></ng-content>
</div>

The toNextStep() method is defined in the AppStepControls component, but it is not called when a button is clicked inside the AppStepControlsComponent tags. How can I call this method when the button is clicked?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan Lopatin, 2016-12-03
@lorus

Add exportAs to the component:

@Component({
  selector: 'app-step-controls',
  template:
  `
  <div>
    <ng-content></ng-content>
  </div>
  `,
  exportAs: 'appStepControls'
})
export class AppStepControlsComponent {
}

Use something like this:
<app-step-controls #ascs="appStepControls">
  <button>Назад</button>
  <button (click)="ascs.toNextStep('test')">Вперед</button>
</app-step-controls>

A
Artem Kayun, 2016-12-05
@Kayun

Use @ViewChild to get child learnangular2.com/viewChild

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question