AngularFormarrayLoadMultipleValue
创始人
2024-10-24 22:01:32
0

使用Angular的Formarray来加载多个值。

具体解法如下:

  1. 在组件的ngOnInit方法中初始化表单控件,包括formGroup和formArray。

  2. 定义一个方法loadData()来加载数据。在这个方法中,可以调用服务从服务器获取数据,然后使用setValue()方法将数据设置到表单控件中。

  3. 在HTML模板中使用ngFor指令遍历表单控件中的每一个条目,并为每个条目定义一个表单项。

示例代码如下:

//定义一个FormGroup formGroup = new FormGroup({ customerName: new FormControl(''), orderItems: new FormArray([]) });

//初始化表单项 ngOnInit() { this.orderService.getOrder().subscribe(response => { let orderItems = response.orderItems.map(orderItem => { //为FormArray添加表单项 return this.createOrderItem(orderItem); }); this.formGroup = new FormGroup({ customerName: new FormControl(response.customerName), orderItems: new FormArray(orderItems) }); }); }

//创建一个表单项 createOrderItem(orderItem: any): FormGroup { return new FormGroup({ itemName: new FormControl(orderItem.itemName), itemPrice: new FormControl(orderItem.itemPrice) }); }

//加载数据 loadData() { this.orderService.getOrder().subscribe(response => { let orderItems = response.orderItems.map(orderItem => { return this.createOrderItem(orderItem); }); this.formGroup.setValue({ customerName: response.customerName, orderItems: orderItems }); }); }

//在模板中使用表单项

相关内容

热门资讯

9分钟了解!wepoker辅助... 9分钟了解!wepoker辅助真的假的(辅助挂)竟然有挂(有挂神器辅助攻略)暗藏猫腻,小编详细说明破...
六分钟了解!hhpoker透视... 六分钟了解!hhpoker透视脚本视频(辅助挂)竟然存在有挂(有挂透明挂辅助技巧)1)有没有挂:进一...
科技新动态!wepoker有插... 科技新动态!wepoker有插件吗(辅助挂)果然真的是有挂(有挂猫腻辅助插件)1、完成辅助器v3.3...
每日必看教程!aapoker辅... 每日必看教程!aapoker辅助工具安全吗(辅助挂)本来真的是有挂(有挂秘诀辅助工具)1、透视辅助软...
揭秘!xpoker辅助器(辅助... 揭秘!xpoker辅助器(辅助挂)都是是真的有挂(有挂分享辅助脚本)运辅助工具,进入游戏界面。进入辅...
实测交流!德普之星辅助软件(辅... 实测交流!德普之星辅助软件(辅助挂)本来是真的有挂(详细教程辅助教程)1、许多玩家不知道辅助怎么退出...
重大消息!wpk软件是正规的吗... 重大消息!wpk软件是正规的吗(辅助挂)果然是真的有挂(有挂技巧辅助教程)1、这是跨平台的轻量版有透...
科技新动态!pokemmo脚本... 科技新动态!pokemmo脚本辅助器下载(辅助挂)一直是真的有挂(有挂总结辅助方法)1、点击下载安装...
分享个大家!wepoker黑侠... 分享个大家!wepoker黑侠破解(辅助挂)果然真的是有挂(揭秘有挂辅助方法)能透视中分为三种模型:...
一分钟快速了解!xpoker辅... 一分钟快速了解!xpoker辅助工具(辅助挂)真是存在有挂(有挂教学辅助app)运辅助工具,进入游戏...