如何获取属性值

时间:2020-07-03 10:23:42

标签: javascript angular typescript

我正在尝试在数据库内部推送复选框的值

我正在使用两个文件(html和ts)

这是我的调查结果。

export class SurveyResultIdComponent implements OnInit {

id: string = "";
surveyDetail = [];
surveyAnswer: FormGroup;


constructor(private router:Router, public route: ActivatedRoute, private fb: FormBuilder) { 
this.id = this.route.snapshot.paramMap.get('id'); //get id parameter
}

ngOnInit() {
this.getSurveyDetail();
this.surveyAnswer = this.fb.group({
  survey: ['test',Validators.required],
  question: ['survey',Validators.required],
  choices: ['choix1',Validators.required],
})
}

getSurveyDetail() {

const apiUrl = 'http://localhost:3000/api';

  axios({
      method: 'get',
      url: `${apiUrl}/survey/show/`+ this.id,
      data: this.surveyDetail
  }).then((response) => {
    this.surveyDetail = response.data;
  });
}

answer() {
const apiUrl = 'http://localhost:3000/api'
axios({
    method: 'post',
    url: `${apiUrl}/survey/answer`,
    data: this.surveyAnswer.value
})
} 
 onSubmit(form:FormGroup){}
}

这是我的survey-result.html:

<tr *ngIf="surveyDetail">
        <td>{{ surveyDetail.name }}</td>
          <td>{{ surveyDetail.questionnaires[0].question }}</td>
        <td>
            <p>
              <label>
                <input type="checkbox" formControlName="choices" value='{{ surveyDetail.questionnaires[0].choices[0].text }}'/>
                <span>{{ surveyDetail.questionnaires[0].choices[0].text }}</span>
              </label>
            </p>
          </td> 
          <td>
            <p>
              <label>
                <input type="checkbox"  formControlName="choices" value='{{ surveyDetail.questionnaires[0].choices[0].text1 }}'/>
                <span>{{ surveyDetail.questionnaires[0].choices[0].text1 }}</span>
              </label>
            </p>
          </td>   
      </tr>
    </tbody>
  </table>
  <div class="answer">
    <button class="btn waves-effect waves-light" type="button" name="action" (click)="answer()">Answer
        <i class="material-icons right">add</i>
    </button>

所以我有一个问题(你喜欢果汁吗?)

您有2个复选框(是和否) 问题是当我选择一个checkox,然后单击答案时。我有“ choix1”,而不是checkox的值

我认为我的问题可能是由于设置了一个值?

如果可以帮助您,这里有结构:

structure

1 个答案:

答案 0 :(得分:0)

因此,基于选择,您的控件将具有不同的值,这就像单选按钮作业,而不是复选框,我会这样做。

   <label>
     <input type="radio" [value]="surveyDetail.questionnaires[0].choices[0].text" formControlName="choices">
       <span>{{surveyDetail.questionnaires[0].choices[0].text}}</span>
   </label>
   <label>
     <input type="radio" [value]="surveyDetail.questionnaires[0].choices[0].text1" formControlName="choices">
       <span>{{surveyDetail.questionnaires[0].choices[0].text}}</span>
   </label>
相关问题