通过@Output传递数据对象

时间:2017-02-17 10:07:38

标签: angular angular2-components angular2-output

我没有收到任何错误,但数据没有传递给父组件。我这样做的方式与我在网上找到的所有例子有点不同,所以我已经不太确定如何设置它了。这是迄今为止的代码。

我如何在private String data1, data2; public ReceivablesSummaryFragment () { // Required empty public constructor } public static ReceivablesSummaryFragment newInstance(String param1, String param2) { ReceivablesSummaryFragment fragment = new ReceivablesSummaryFragment (); Bundle args = new Bundle(); args.putString("first data", param1); args.putString("second data", param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { data1= getArguments().getString("first data); data2= getArguments().getString("second data"); } }

中调用它
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

这是我理性化的最好方法,试图让这一切都有效。我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

如果提供字段或方法名称,事件绑定不会分配或调用,它只是执行表达式。因此,表达式需要包括赋值或调用。 $event引用了发出的值。

(selectedO)="selectedI = $event" // field

(selectedO)="selectedI($event)" // method