2017-06-06 60 views
0

我有父和子組件,從子需要觸發父組件功能。輸出事件發射器觸發只在ngOnInit

Parent.component.ts

loadData(event) { 
     console.log(event); 
} 

Parent.component.html

<app-project (uploaded)="loadData($event)" ></app-project> 

child.component.ts

@Output() uploaded:EventEmitter<any> = new EventEmitter(); 

ngOnInit() { 
    this.uploaded.emit('complete'); // Worked 
} 

loadProject(){ 
    this.uploaded.emit('complete'); // Not triggering the parent function 
} 

child.component.html

<button type="button" (click)="loadProject(project)" label="Load Project"></button> 

不知道什麼是錯在這一點,但是從ngOnInit workes。

從child.component.html調用loadProject。

+0

從哪裏調用loadProject()? –

+0

有一個函數(editProject)在child.component.ts從那裏調用 editProject(project:Project){ this.uploaded.emit('complete'); } –

+1

也許編輯你的問題是一個更好的主意,因爲評論中的代碼很難閱讀。還請在調用'editProject()'的地方添加信息。 –

回答

2

變化:

child.component.ts

@Output() uploaded = new EventEmitter<string>(); 

ngOnInit() { 
    this.uploaded.emit('complete'); // Worked 
} 

loadProject(){ 
    this.uploaded.emit('complete'); // Not triggering the parent function 
} 

child.component.html

<button type="button" (click)="loadProject()" label="Load Project"></button> 

你是傳遞一個字符串值loadProject,但在孩子組件沒有負載項目的定義,它取1個參數