在我的代码选项数组中不起作用,当我尝试将其与ngModel绑定时,它对我不起作用,并给了我400错误的请求。我在这里包含我的代码
这是我的2个模型问题模型和选项模型
Question.ts
import { Option } from '../models/option';
export class Question {
id: number;
name: string;
options: Option[] = new Array(4);
constructor(data = null) {
if (data) {
this.id = data.id;
this.name = data.name;
this.options = data.options;
}
}
}
Option.ts
export class Option {
id: number;
questionId: number;
option: string;
isAnswer : boolean;
constructor(data = null) {
if(data){
this.id = data.id;
this.questionId = data.questionId;
this.option = data.option;
this.isAnswer = data.isAnswer;
}
}
}
我在这里包含了exam.component.ts代码
class FormExam {
id: number;
name: string;
options: Option[] = new Array(4);
static formexam(question: Question) {
let instance = new this;
instance.id = question.id;
instance.name = question.name;
instance.options = question.options;
return instance;
}
toTask(question: Question) {
question.id = this.id;
question.name = this.name;
question.options = this.options;
return question;
}
}
export class ExamComponent implements OnInit {
@Input() question: Question;
@Output() onSubmit = new EventEmitter();
userModel: User;
form: FormGroup;
dialogHeader2: string;
isNewTask: boolean;
filterStr: string = '';
isRequestLoading: boolean;
isValidateLoading: boolean;
model: FormExam;
showNameError: boolean;
stateModel: any;
stateText: string;
submitButtonText2: string;
questionModel: Question;
ngOnInit() {
let task = this.question;
this.isNewTask = !task;
this.question = task ? task : new Question();
this.submitButtonText2 = this.question.id ? 'Save' : 'Create';
this.model = FormExam.formexam(this.question);
}
这是html代码,当我在选项中使用[[ngModel)]且isAnswer对我不起作用时,此代码中。
<div class="ct-form-block">
<div>
<label class="ct-form-label">Question</label>
<ct-textarea [name]="'name'" [maxlength]="200" [(ngModel)]="model.name"> </ct-textarea>
</div>
<div *ngFor="let option of model.options;">
<label class="ct-form-label"> Options</label>
<div class="ct-required-field-container ">
<input class="ct-input ct-full-width2"
name="options" [(ngModel)]="option.option"/>
<input type="checkbox" name="Options" [(ngModel)]="option.isAnswer" />
</div>
</div>
</div>
在此代码中,model.option和model.isAnswer无效,因为当我提交此代码时,它给我错误的请求400错误,因此还有其他方法可以将该值存储在数据库中。
答案 0 :(得分:0)
以下内容:
<input type="checkbox" name="Options" [(ngModel)]="model.isAnswer" />
这不是(循环遍历什么):
<input type="checkbox" name="Options" [(ngModel)]="option.isAnswer" />