传递数据-对象从子对象到父Angular 6

时间:2018-12-12 09:14:51

标签: parameter-passing angular6 eventemitter

在子组件中,我有一个数据表,当我单击该行时,我将获取数据并将其保存在branchToDivision中,我还有一个按钮,当我按下该按钮时,我可以发送{{ 1}}到父组件。

子组件

branchToDivision

父项

@Output() messageEvent: EventEmitter<BranchDto> = new EventEmitter<BranchDto>();
branchToDivision: BranchDto;
onSelect(record: BranchDto) {
        this.branchToDivision = new BranchDto();
        this.branchToDivision = record;
        console.log(this.branchToDivision);
        console.log(this.branchToDivision.brancH_CODE);
    }
acceptBranch() {
        this.onSelect(this.branchToDivision);
        this.messageEvent.emit(this.branchToDivision);
        this.branchModalDivision.hide();        
    }

父HTML

branch: BranchDto;
getBranch(branch: BranchDto): void{
        this.branch = branch;
        console.log(this.branch);
        console.log(this.branch.brancH_CODE);       
    }

我尝试记录分支属性,但未定义,怎么了?任何想法都对我有帮助。

1 个答案:

答案 0 :(得分:2)

这是一种将信息从子组件发送到父组件的方法:

parent.component.html:

<div>
    <child (getData)="getData($event)"></child>
</div>

在parent.component.ts中:

public getData(value): void {
    console.log(value) // welcome to stackoverflow!
}

在child.component.ts中:

import {Output, EventEmitter} from '@angular/core';

@Output() public getUserData = new EventEmitter<string>();

this.getUserData.emit('welcome to stackoverflow!');

我希望我的帮助有效effective