Angular 角度/如何确保不完整的服务器模型获胜';不打破NGM模式?
以下控件的值绑定到employee对象。如果服务器不将人员返回给员工,我如何确保这不会中断Angular 角度/如何确保不完整的服务器模型获胜';不打破NGM模式?,angular,Angular,以下控件的值绑定到employee对象。如果服务器不将人员返回给员工,我如何确保这不会中断 <input type="text" class="form-control" name="employee-name" [(ngModel)]="employee.person.details.name" required> 是否有一种方法可以动态创建缺少的对象路径?您可以为员工创建一个模型类,并使用空白的人员详细信息名称启动它,然后使用该模型类实例化员工属性 export clas
<input type="text" class="form-control" name="employee-name" [(ngModel)]="employee.person.details.name" required>
是否有一种方法可以动态创建缺少的对象路径?您可以为员工创建一个模型类,并使用空白的人员详细信息名称启动它,然后使用该模型类实例化员工属性
export class YourClass {
employee: Employee = new Employee(); //default constructor
...
service.getEmployee().subscribe((res) => this.employee = new Employee(res)); //custom constructor
...
}
返回并实际查看我的代码:-)。。。我看到我有一个initialize方法,它为此返回一个初始化对象:
initializeProduct(): IProduct {
// Return an initialized object
return {
id: 0,
productName: null,
productCode: null,
category: null,
tags: [],
releaseDate: null,
price: null,
description: null,
starRating: null,
imageUrl: null
};
}
您的需要更加复杂,因为它有更多级别的子对象。但你也可以做类似的事情。我在创建“新”条目时使用此选项,以确保不会看到“未定义”错误。这不是强制转换吗?是吗?@TrevorHector是的,可能是