Bonjour les amis,

je veux faire une communication parent to child avec la méthode chieldView
afin qu'un composant parent puisse appeler une fonction d'un composant enfant.

Pour cela, j'ai respecté la doc officielle :
https://angular.io/docs/ts/latest/co...ountdown-tests

le composant enfant qui est: login.component.ts
ou j'y ai mis une simple fonction test :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
...
test() {
  console.log("test");  
}
et le composant parent : app.componnt.ts qui doit appeler test() de login.component.ts
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { Component, OnInit, Input, AfterViewInit, ViewChild } from '@angular/core';
import { LoginComponent } from './login/login.component';
 
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, AfterViewInit  {
 
  @ViewChild(LoginComponent)
  private loginComponent: LoginComponent;
 
  ngOnInit() {
    this.lancerTest();
  }
  ngAfterViewInit() {
  }
 
  lancerTest() {
      this.loginComponent.test();        //appelle de la fonction test() du composant :  login.component
  }
 
}


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
error_handler.js:48 EXCEPTION: Error in ./AppComponent class AppComponent_Host - inline template:0:0 caused by: Cannot read property 'test' of undefinedErrorHandler.handleError @ error_handler.js:48(anonymous function) @ application_ref.js:236ZoneDelegate.invoke @ zone.js:232onInvoke @ ng_zone.js:238ZoneDelegate.invoke @ zone.js:231Zone.run @ zone.js:114(anonymous function) @ zone.js:502ZoneDelegate.invokeTask @ zone.js:265onInvokeTask @ ng_zone.js:229ZoneDelegate.invokeTask @ zone.js:264Zone.runTask @ zone.js:154drainMicroTaskQueue @ zone.js:401
error_handler.js:50 ORIGINAL EXCEPTION: Cannot read property 'test' of undefined