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

bao*_*nhg 1 parameter-passing eventemitter angular6

在子组件中,我有一个数据表,当我单击该行时,我将获取数据并将其保存在 中branchToDivision,我还有一个按钮,当我点击该按钮时,我可以发送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();        
    }
Run Code Online (Sandbox Code Playgroud)

父组件

branch: BranchDto;
getBranch(branch: BranchDto): void{
        this.branch = branch;
        console.log(this.branch);
        console.log(this.branch.brancH_CODE);       
    }
Run Code Online (Sandbox Code Playgroud)

父 HTML

<branchModal #branchModal (messageEvent)="getBranch($event)" ></branchModal>
Run Code Online (Sandbox Code Playgroud)

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

小智 5

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

parent.component.html:

<div>
    <child (getData)="getData($event)"></child>
</div>
Run Code Online (Sandbox Code Playgroud)

在 parent.component.ts 中:

public getData(value): void {
    console.log(value) // welcome to stackoverflow!
}
Run Code Online (Sandbox Code Playgroud)

在 child.component.ts 中:

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

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

this.getUserData.emit('welcome to stackoverflow!');
Run Code Online (Sandbox Code Playgroud)

我希望我的帮助有效吗?