angular - 如何设置输入角度的默认值?
问题描述
我在组件中有输入:
@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:
}}`
解决方案
当我们使用类时
@Input() applicant: Applicant = new Applicant();
当我们使用接口时:
@Input() applicant: Applicant = {} as Applicant;
推荐阅读
- docker - kubeadm 初始化错误:kubeadm 等待条件超时
- php - 标头函数php不会使用发布请求重定向
- java - EXTRA_SUPPORTED_LANGUAGES 列表是否在 Android 设备的整个生命周期内都是固定的?
- android-external-storage - Google Play 通知 - 外部存储权限 - 2021 年 5 月 5 日之前
- assembly - 为什么我输入不同的值输入时总是出现最后一条消息
- java - 有没有一种惯用的方法来断言流只有一个元素而产生该元素?
- javascript - 通过 JavaScript 中的钱包连接到 Oracle 云数据库?
- mysql - 使用 spring boot JPA hibernate 存储库检查用户名和密码的 Mysql 查询不起作用。我应该怎么办?
- docker - 将我的 ktor 应用程序部署到 docker 时运行 .jar 文件时出现问题
- c# - 使用带有 DTO 的 HttpPost,邮递员无法调用 tht 方法