我在Angular / Nativescript中有一个动态生成的清单,如下所示:
<StackLayout *ngIf="assessment">
<StackLayout *ngFor="let instance_item of assessment.exam_instance_items; let i= index">
<ns-examitem [attr.id]="'item_'+instance_item.id" [assessmentitem]="instance_item"
(changeEvent)="validateExam($event)"></ns-examitem>
</StackLayout>
</StackLayout>
示例项具有绑定到值的单选按钮控件
(在exaitem.component.ts中为子组件)
// get the values out
public selectedvalue: string = '';
// tell the world something's changed, trigger validation
@Output() changeEvent = new EventEmitter();
changeCheckedRadio(radioOption: RadioOption): void {
// uncheck all other options
this.radioOptions.forEach(option => {
if (option.value !== radioOption.value) {
option.selected = false;
} else {
option.selected = true;
this.selectedvalue = option.value;
this.assessmentitem.selectedValue = this.selectedvalue;
}
});
// tell the world the value has changed
console.log('ExamitemComponent.Radio option changed to:' + this.selectedvalue)
this.changeEvent.emit();
}
我真的很难从父组件中的exitem组件中获取selectedvalue。我可以通过ID获取组件引用,但无法读取该值。
有人可以帮助吗?
答案 0 :(得分:0)
您必须像这样在父组件中发出要获取的值
this.changeEvent.emit(this.selectedvalue);
然后在父组件中,可以在validateExam函数中使用此值。