首页 > 解决方案 > 如何设置输入角度的默认值?

问题描述

我在组件中有输入:

  @Input() applicant: Applicant;

interface Applicant {
   applicant: ApplicantItem,
   representative: Representative
}

模板是:

<div *ngIf="applicant.representative.type></div>

问题是如果applicant没有物体representative它会掉下来。

如果没有,如何设置默认值representative

像这样:

@Input() applicant: Applicant = <Applicant>{};

我需要永远representative在一起type

还有如何在这里避免错误:

applicant.representative = adapt(applicant.representative);

这里applicant.representative应该是默认对象{ representative: {type: }}`

标签: angular

解决方案


当我们使用类时

@Input() applicant: Applicant = new Applicant();

当我们使用接口时:

@Input() applicant: Applicant = {} as Applicant;

推荐阅读