2017-06-06 62 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个参数