首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在angular2中访问窗体数组中控件的值

在Angular 2中,要访问窗体数组中控件的值,可以使用FormArrayFormGroup来管理表单控件。以下是一种方法:

  1. 首先,在组件类中导入必要的模块和类:import { Component, OnInit } from '@angular/core'; import { FormGroup, FormControl, FormArray } from '@angular/forms';
  2. 在组件类中创建一个表单组:@Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'] }) export class MyComponentComponent implements OnInit { myForm: FormGroup; constructor() { } ngOnInit() { this.myForm = new FormGroup({ controlsArray: new FormArray([]) }); } }
  3. 在模板中使用formArrayName指令来绑定表单组:<form [formGroup]="myForm"> <div formArrayName="controlsArray"> <div *ngFor="let control of myForm.get('controlsArray').controls; let i = index"> <input type="text" [formControlName]="i"> </div> </div> </form>
  4. 现在,你可以通过myForm.get('controlsArray').value来获取窗体数组中控件的值。例如,你可以在组件类中添加一个方法来获取这些值:getControlValues() { console.log(this.myForm.get('controlsArray').value); }

这样,当你调用getControlValues()方法时,控制台将打印出窗体数组中控件的值。

请注意,上述示例中使用了Angular的响应式表单模块(@angular/forms)。如果你还没有导入该模块,请确保在使用之前先导入它。

关于Angular表单的更多信息,你可以参考腾讯云的相关文档和教程:

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券