我没有遇到任何错误,但是数据并未传递到父组件。我的操作方式与我在网上找到的所有示例有所不同,因此我已经不太确定如何设置它。这是到目前为止的代码。
我在Child Component
//a variable that stores the fetched data from a group of radio buttons
selected = {value1: '', value2: ''};
//the output variable
@Output() selectedO: EventEmitter<any> = new EventEmitter();
public sendAnswer = (): void => {
this.selectedO.emit(this.selected);
}
Child Component
模板中的输入
<input type="radio"
[attr.name] = "quesForm.value.name"
[attr.id] = "ans.id"
[attr.value] = "ans.answer"
(click) = "getSelected(ans)" //fetches data I want to pass
(click) = "sendAnswer()" //sets fetched data to @Output
hidden
/>
我在Parent Component
模板中如何调用它
<multiple-choice-radio *ngIf="expbTrigger"
[question]="question01"
(selectedO)="selectedI" //where I'm trying to pull the data into parent component
></multiple-choice-radio>
我如何在Parent Component
public selectedI(selected) {
this.selectedII = selected;
}
selectedII: any; //the variable I'm trying to store it into
这是我可以合理地尝试使这一切工作的最好方法。我在这里做错了什么?
事件绑定如果提供字段或方法名称,则不会分配或调用,它只是执行表达式。因此,表达式需要包括作业或呼叫。$event
参考发射值。
(selectedO)="selectedI = $event" // field
或
(selectedO)="selectedI($event)" // method