Nativescript 从 ng-repeat 中的子元素获取属性
Nativescript get properties from child element in ng-repeat
我在 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>
其中考试项目有一个单选按钮控件绑定到一个值
(在 examitem.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();
}
我真的很难从父组件的 examitem 组件中获取 selectedvalue。我可以通过 ID 获取组件引用,但无法读取值。
有人能帮忙吗?
你必须像这样发出你想在父组件中获得的值
this.changeEvent.emit(this.selectedvalue);
然后在父组件中,您可以在 validateExam 函数中使用此值。
我在 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>
其中考试项目有一个单选按钮控件绑定到一个值
(在 examitem.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();
}
我真的很难从父组件的 examitem 组件中获取 selectedvalue。我可以通过 ID 获取组件引用,但无法读取值。
有人能帮忙吗?
你必须像这样发出你想在父组件中获得的值
this.changeEvent.emit(this.selectedvalue);
然后在父组件中,您可以在 validateExam 函数中使用此值。