typescript - Angular 中的模型
问题描述
我有这个模型
export class model{
Id: string;
Name: string;
home: Home[];
}
export class Home{
street: string;
country: string;
CP: string;
}
问题是如何在父模型中使用模型 Home 的值并使用表单中的值插入新寄存器,例如:
<form>
<input type="text" ([ngModel])="model.id">
<input type="text" ([ngModel])="model.Name">
<input type="text" ([ngModel])="model.Home.CP"><!--How to implements the values of home to insert a value in the BD-->
</form>
谢谢
解决方案
@胡安佩雷斯
要在类中使用Home[]
数组model
,您需要在model
类构造函数中实例化数组。然后在将数据绑定到类的home
属性时model
,必须在home
数组中使用索引。可能的代码如下所示
<input type="text" ([ngModel])="model.home[index].CP">
在model
类中,构造函数应如下所示
export class model{
Id: string;
Name: string;
home: Home[];
model() {
home = new Array(3).fill(new Home());
}
}
export class Home{
street: string;
country: string;
CP: string;
Home() {
street = "";
country = "";
CP = "";
}
}
推荐阅读
- python - 有没有办法修复这段代码中的最大深度错误
- angular - 将 ccavenue 支付集成与 laravel 和 angular 7 集成
- vb.net - 如何将 Databound Gridview 转换为 Datatable?
- wordpress - 在 wordpress 主题中使用 w3 svg 图标
- arduino - 如何诊断 ESP8266/D1 迷你重启循环问题?
- dolphindb - 如何在 DolphinDB 中将集合转换为向量
- sql - SQL Server 2016 - 将整数列转置为逐日行
- javascript - 两次呼叫提示问题
- ios - 如何在 SWIFT 5 中更改对象结构的值
- android - 如何创建一个自定义片段,用它自己的后栈扩展 NavHostFragment?