输出事件发射器仅在ngOnInit上触发

时间:2017-06-06 06:37:09

标签: angular typescript

我有父子组件,从子组件需要触发父组件功能。

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。

从child.component.html调用loadProject。

1 个答案:

答案 0 :(得分: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个参数的loadproject定义