【问题标题】:How to store array value in database with angular 5如何使用角度5将数组值存储在数据库中
【发布时间】:2019-02-27 15:33:49
【问题描述】:

在我的代码选项数组中不起作用,当我尝试将它与 ngModel 绑定时,它对我不起作用并给我 400 错误请求。我在这里包含我的代码

这是我的 2 模型问题模型和选项模型

问题.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; 
    }  
}
}

选项.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 错误,所以有没有其他方法可以将此值存储在数据库中。

【问题讨论】:

  • 这是您的服务器引发的错误。检查服务器日志为什么请求没有被接受。
  • 我不认为这是服务器端错误,因为当我的应用程序运行时,由于模型错误,它也没有显示创建按钮。@Henry

标签: angular typescript angular5 angular2-forms angular4-router


【解决方案1】:

以下内容:

<input type="checkbox" name="Options" [(ngModel)]="model.isAnswer" />

这不应该是(你的循环迭代的内容):

<input type="checkbox" name="Options" [(ngModel)]="option.isAnswer" />

【讨论】:

    猜你喜欢
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    相关资源
    最近更新 更多