Angular 在“角度”中,为嵌套对象指定值
我正在研究Angular 2应用程序。我有嵌套对象“子问题”的对象。我需要给它赋值,但无法识别作为子问题ID的错误。subQuestionId是子问题的属性 错误 数据类 组成部分Angular 在“角度”中,为嵌套对象指定值,angular,typescript,Angular,Typescript,我正在研究Angular 2应用程序。我有嵌套对象“子问题”的对象。我需要给它赋值,但无法识别作为子问题ID的错误。subQuestionId是子问题的属性 错误 数据类 组成部分 子问题是尚未实例化的对象 您可以通过以下方式消除此错误: answerData.subQuestions = { subQuestionId: subQuestionId }; 您得到的错误到底是什么?TypeError:无法设置未定义的属性'subQuestionId' export class AnswerEn
子问题
是尚未实例化的对象
您可以通过以下方式消除此错误:
answerData.subQuestions = { subQuestionId: subQuestionId };
您得到的错误到底是什么?TypeError:无法设置未定义的属性'subQuestionId'
export class AnswerEntryDataModel{
consultationId:string;
responseId:string;
questionId:string;
answers:string[];
isPreDefineAnswer:boolean;
subQuestions:{
subQuestionId:string;
isPreDefineSubAnswer:boolean;
subQuestionAnswers:string[];
}
}
export class myComponent implements OnInit {
private answerData = new AnswerEntryDataModel();
private answersList:string [] = [];
private subQuestionAnswersList:string [] = [];
private assembleAnswer( responseId:string, questionId:string, subQuestionId:string, subQuestionInputType:string, subQuestionAnswersList:string[] ):AnswerEntryDataModel
{
this.answerData.answers = [];
this.answerData.consultationId = this.session.getItem('consultationId');
this.answerData.responseId = responseId;
this.answerData.questionId = questionId;
if(subQuestionId!=null || subQuestionId!="undefined")
{
answerData.subQuestions.subQuestionId = subQuestionId; //throw error from here
if(subQuestionInputType =="textbox"){
this.answerData.subQuestions.isPreDefineSubAnswer = false;
}
else {
this.answerData.subQuestions.isPreDefineSubAnswer = true;
}
if(subQuestionAnswersList.length>0)
{
for(var itemIndex in subQuestionAnswersList)
{
this.answerData.subQuestions.subQuestionAnswers.push(subQuestionAnswersList[itemIndex]);
}
}
}
console.log("***** complete answer data ", this.answerData);
return this.answerData;
}
answerData.subQuestions = { subQuestionId: subQuestionId };