Javascript 未定义的角度对象数组值抛出错误
我正在研究Angular 6应用程序。我有输入的行为变量,一旦我收到数据,我就映射到surveyInfo对象。我已经调查了InFoDataModel类,如下所示;接下来,我试图通过读取模板中的surveyInfo对象来显示此数据,但出现错误 错误 成分 HTML模板Javascript 未定义的角度对象数组值抛出错误,javascript,angular,typescript,angular6,Javascript,Angular,Typescript,Angular6,我正在研究Angular 6应用程序。我有输入的行为变量,一旦我收到数据,我就映射到surveyInfo对象。我已经调查了InFoDataModel类,如下所示;接下来,我试图通过读取模板中的surveyInfo对象来显示此数据,但出现错误 错误 成分 HTML模板 {{surveyInfo.surveyId} 尝试将if(survey!=null)更改为if(!survey)return。如果没有调查原因返回语句不在括号内,则返回未定义。如果它能工作,你需要在未定义的上检查该对象的所有道具。
{{surveyInfo.surveyId}
尝试将if(survey!=null)
更改为if(!survey)return代码>。如果没有调查
原因返回语句不在括号内,则返回未定义
。如果它能工作,你需要在未定义的
上检查该对象的所有道具。您还需要将键入添加到此对象
private mapSurveyInfo(survey:any):SurveyInfoDataModel{
if (!survey) return;
this.surveyInfo = new SurveyInfoDataModel(
survey.consultationId,
survey.surveyId,
survey.surveyIdNum,
survey.surveyName
);
return this.surveyInfo;
}
调查
在您的案例中未定义。如果survey
为空,您可以使用以下方法测试空值和未定义值,而不是进行测试:
if(!!survey){
this.surveyInfo = new SurveyInfoDataModel(
survey.consultationId,
survey.surveyId,
survey.surveyIdNum,
survey.surveyName
);
}
如果将if(survey!=null){
更改为if(typeof survery!=='undefined'&&survey!==null),会发生什么情况
?我仍然有相同的错误…我正确地映射了,我可以在这里看到数据。surveyInfo调试器,这就是为什么我对哪里出错感到困惑。你确定你的observablesurveySelectedToGetInfo
返回一个值吗?是的,确认…我可以在调试器中看到数据我认为我在模板中有问题…我用t更新了我的问题模板块…如果你可以参考的话..谢谢我想我在模板中有问题..我用模板块更新我的问题..如果你可以参考的话..谢谢,模板没问题,试试我的代码片段
export class SurveyInfoDataModel{
surveyId:string;
surveyIdNum:string;
surveyName:string;
consultationId:string;
constructor(consultationId, surveyId, surveyIdNum, surveyName ){
this.consultationId =consultationId;
this.surveyId = surveyId;
this.surveyIdNum = surveyIdNum;
this.surveyName = surveyName;
}
}
<div class="surveyListInfoBlock">
<div *ngIf="surveyInfo">
{{surveyInfo.surveyId}}
</div>
</div>
private mapSurveyInfo(survey:any):SurveyInfoDataModel{
if (!survey) return;
this.surveyInfo = new SurveyInfoDataModel(
survey.consultationId,
survey.surveyId,
survey.surveyIdNum,
survey.surveyName
);
return this.surveyInfo;
}
if(!!survey){
this.surveyInfo = new SurveyInfoDataModel(
survey.consultationId,
survey.surveyId,
survey.surveyIdNum,
survey.surveyName
);
}