角度4中的父子关系

时间:2018-04-12 04:35:27

标签: angular

这是我提交后想要的db.json

{
    "id": 0,
    "code": "string",
    "name": "string",
    "isActive": true,
    "questionerdetails":[
        {
            "id":0,
            "questiondetail": "string",
            "isActive": true
        }
    ]
}

这是我在questioner.component.ts中的方法

formSubmits() {
    this.validate = true;
    this.preProcessConfigurations();
    let questioner = this.questionerForm.value;
    let question = this.questions;

    this.allquestionare.forEach(obj => {

      question.push({ 'questiondetail': '' })
    })

    console.log(questioner);
    this.questionareService.getAllQuestioner()
      .subscribe(questionerss => {

        this.questionareService.createQuestioner(questioner)
          .subscribe(successCode => {
            this.statuscode = successCode;
            this.questionerForm.reset();
            alert("Success");

          },
          errorCode => this.statuscode = errorCode
          );
      });

  }

请帮忙 - 我被困在方法表单提交中,将子类questionerdetails推送到父类

1 个答案:

答案 0 :(得分:0)

我有类似的要求。我已经使用了这种方法,我创建了两个模型。在你的情况下说QuestionnaireQuestionnaireDetails。进入Questionnaire会有QuestionnaireDetails的数组。您可以使用构造函数或setter / getters来设置/获取数据。

检查下面给出的例子,

调查问卷模型

export class Questionnaire {

    private id: number;
    private code: string;
    private name: string;
    private isActive;
    private questionerdetails: QuestionnaireDetails[];

    constructor(){
    }
}

调查问卷详情模型

export class QuestionnaireDetails {

    private id: number;
    private questionnairedetail: string;
    private isActive;

    constructor(){
   }
}