Nativescript从ng-repeat中的子元素获取属性

时间:2019-03-05 07:45:54

标签: angular nativescript nativescript-angular

我在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获取组件引用,但无法读取该值。

有人可以帮助吗?

1 个答案:

答案 0 :(得分:0)

您必须像这样在父组件中发出要获取的值

this.changeEvent.emit(this.selectedvalue);

然后在父组件中,可以在validateExam函数中使用此值。