angular - 我想在已经渲染的 Angular 组件中重用表单单选按钮元素
问题描述
我有一个案例可以从问题列表中一次显示一个问题,并带有一个单选按钮来捕获响应为是或否。
我的方法:
我正在question = "{currentId : 0, questionId : 'value', question : 'This is the question text?"}
组件的打字稿代码中初始化一个 javascript 对象。
在当前组件的 HTML 模板中,我已将名称属性设置为的单选按钮设置"question.questionId"
为
单击下一步按钮时,我提交表单并将数据附加到包含“[{questionId“value”,response:“true/false”}]”的响应列表中,并将问题对象的值替换为增量"question.currentId"
1和从问题列表的下一个元素中获取数据。
在提交下一个问题时,我发现单选按钮元素的名称属性值对于连续问题保持不变。
我无法弄清楚为什么 name 属性中的值没有改变。但是,HTML 中问题文本的显示会随着计数的增加而变化。
请为此提供解决方案
问题.component.html:
<form class="form-horizontal" (ngSubmit)=onSubmit() #questionForm='ngForm'>
<div class="card-body">
<label class="col-md-12 col-form-label">{{ question.question1 }}</label>
<div class="form-group row">
<div class="col-md-9 col-form-label">
<div class="form-check">
<input class="form-check-input" type="radio" name= "question{{ question.questionId }}" value="true" ngModel>
<label class="form-check-label" for="radio1">
Yes
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name= "question{{ question.questionId }}" value="false" ngModel [ngModel] = "checked">
<label class="form-check-label" for="radio2">
No
</label>
</div>
</div>
</div>
</div>
<div class="card-footer">
<button type="button" class="btn btn-sm btn-success" (click) = "onPrevious()" [disabled] = "question.currentId === 0">
<i class="fa fa-arrow-left"></i> Previous</button>
<button type="submit" class="btn btn-sm btn-success" [disabled] = "question.currentId === questions.length">
<i class="fa fa-arrow-right"></i> Next</button>
<button type="button" class="btn btn-sm btn-success" style="float: right;" (click) = "submitComponentResponses()">
<i class="fa fa-ban"></i> Submit</button>
</div>
</form>
问题.component.ts:
private getComponentAndQuestions() {
this.questionService.getQuestionsData(projectAssessmentId).subscribe(q => {
this.questions = q;
this.question = {
currentId: 0,
questionId: this.questions[0].questionId,
question1: this.questions[0].question1,
quizId: this.questions[0].quizId
}
}, error => {
this.exceptionService.errorHandler(error);
});
}
onSubmit() {
if (this.questionForm.valid) {
let UserProjectId = this.userProject.id;
Object.keys(this.questionForm.form.controls).forEach(
(key) => {
if (this.response.some((item) => item.QuesId == key)) {
this.response.splice(this.question.currentId, 1, {
QuesId: key,
Response: this.questionForm.form.get(key).value,
UserProjectId: this.userProject.id
});
} else {
this.response.push({
QuesId: key,
Response: this.questionForm.form.get(key).value,
UserProjectId: UserProjectId
});
}
}
);
//Increment the current Id and reset question object to next element
//from array of questions.
if (this.question.currentId < this.questions.length - 1) {
this.setCurrentQuestion(this.question.currentId + 1);
}
}
}
private setCurrentQuestion(currId: number) {
this.question = {
currentId: currId,
questionId: this.questions[currId].questionId,
question1: this.questions[currId].question1,
quizId: this.questions[currId].quizId
}
}
解决方案
尝试设置name
属性角度方式:
[name]="'question' + question.questionId" or
[attr.name]="'question' + question.questionId"
推荐阅读
- kotlin - 找不到 org.jetbrains.kotlin:kotlin-gradle-plugin:1.4-M2
- php - Laravel 身份验证未重定向到预期的仪表板
- python - python中的mdo_import_help在哪里
- angular - 带有 ng-template 的 Angular 内容子项不起作用
- c - 通过一维数组初始化程序初始化未知大小的二维数组是否符合标准?
- javascript - 如果版本不同,如何在生产中比较 react js 中的构建哈希以清除缓存?
- android - Google AdMob Unity 插件无法在旧的 Android 版本上运行 - ClassNotFoundException - 这是预期的行为吗?
- php - 如何根据用户名/学号显示只能由特定用户下载的文件?
- python - 如何为我的机器学习模型将数据从形状 (256, 60) 转换为 (96, 64)?
- python - Pyqt5 将鼠标悬停在其上时更改 qpushbutton 样式表