attr.data在角度5返回null

时间:2018-06-27 12:18:09

标签: angular radio-button getelementbyid attr

我一直在尝试获取所选单选按钮的ID值,为此,我使用了 attr.data-formr ,但是在通过函数 confirmed1()传递值之后,它返回值 NULL 。我想要实现的是获取在输入标签中定义的attr.data-formr的值。可能有什么问题吗?

HTML代码:

<form [formGroup]="posTrial" #q1> 
   <label class="labl">
    <input type="radio" 
     [attr.data-formr]=1 
     class="form-control" 
     formControlName="pos1"  
     value={{quizData[0].option1}}>
    <div class="option">{{quizData[0].option1}}</div>
   </label>
  <label class="labl">
    <input type="radio" 
      [attr.data-formr]=2  
      class="form-control"  
      formControlName="pos1"  
      value={{quizData[0].option2}}>
    <div class="option">{{quizData[0].option2}}</div>
  </label>
  <label class="labl">
    <input type="radio" 
      [attr.data-formr]=3 
       class="form-control" 
       formControlName="pos1"  
       value={{quizData[0].option3}}>
     <div class="option">{{quizData[0].option3}}</div>
   </label>
    <label class="labl">
      <input type="radio" 
        [attr.data-formr]=4 
        class="form-control" 
        formControlName="pos1"  
        value={{quizData[0].option4}}>
      <div class="option">{{quizData[0].option4}}</div>
    </label>
    <div *ngIf="toggle1!=1 ; then template1; else template2" style="display:inline"></div>
    <ng-template #template1>
        <button mat-raised-button color="primary" style="width:100%;"  (click)="confirmed1(this.posTrial.value.pos1,q1.getAttribute('data-formr'))" [disabled]="!posTrial.dirty" class="footer">Confirm</button>
    </ng-template>
    <ng-template #template2>
        <button mat-raised-button color="primary" style="width:100%;" class="footer1" (click)="selectNext(matgroup)" >Next</button>
        <button mat-raised-button color="primary" style="width:100%;"  class="footer2" (click)="openDialog(quizData[0].info)" >Show Details</button>
    </ng-template>
 </form>

角度代码:

confirmed1(ans:any,id:any)
      {
        console.log(ans);
        if(ans==this.ansData[1].answer)
        {
          console.log("tango successful");
          this.toggle1Cor=1;
          console.log(id);

        }
        else{
          console.log("Charlie SOS");
          this.toggle1Cor=2;
          console.log(id);

        }
        this.toggle1=1;
        this.posTrial.get('pos1').disable()
        this.dis=1;
        console.log(id);   
  }

控制台结果

option11              page-questions.component.ts:178 
Charlie SOS           page-questions.component.ts:180 
null                  page-questions.component.ts:186

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我不是从属性中收集数据。相反,我这样做的方法是完全错误的。我必须使用#[derive(Debug, Deserialize)] struct Item { #[serde(skip)] name: String, some_field: u32, } while let Some((key, value)) = access.next_entry::<String, Item>()? { items.push(Item::new(key, value.some_field)); } 来获取属性的特定值