Angular响应式表单数组,改变检测覆盖表单。
创始人
2024-10-30 07:01:09
0

Angular的响应式表单数组可以使用FormArray来实现,通过改变检测覆盖表单可以使用ChangeDetectionStrategy来设置。

首先,创建一个响应式表单数组:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit {
  myForm: FormGroup;

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.myForm = this.fb.group({
      items: this.fb.array([])
    });
  }

  get items(): FormArray {
    return this.myForm.get('items') as FormArray;
  }

  addItem() {
    this.items.push(this.fb.control(''));
  }

  removeItem(index: number) {
    this.items.removeAt(index);
  }
}

在模板中,你可以使用FormArray来渲染表单数组,并可以通过调用addItem和removeItem方法来添加或删除项:

要改变检测覆盖表单,你可以在组件中设置ChangeDetectionStrategy:

import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyComponentComponent implements OnInit {
  // ...
}

通过将ChangeDetectionStrategy设置为OnPush,你可以手动控制何时触发变更检测,以提高性能。

相关内容

热门资讯

透视总结!sohoo poke... 透视总结!sohoo poker辅助器(透视)三哥玩十三张辅助器(本来存在有辅助辅助器)-哔哩哔哩;...
透视规律!wepoker辅助是... 透视规律!wepoker辅助是真的假的(透视)大唐麻将辅助软件(都是真的是有辅助下载)-哔哩哔哩1、...
透视数据!竞技联盟辅助(透视)... 透视数据!竞技联盟辅助(透视)杭麻圈辅助工具(果然是有辅助插件)-哔哩哔哩1)杭麻圈辅助工具辅助插件...
透视存在!wepoker黑侠辅... 透视存在!wepoker黑侠辅助器(透视)雀神广东麻将推倒胡辅助器下载(竟然有辅助app)-哔哩哔哩...
据目击者称!pokemmo手机... 据目击者称!pokemmo手机版透视脚本(透视)授权二厅辅助(果然真的是有辅助修改器)-哔哩哔哩1、...
透视总结!werplan免费挂... 透视总结!werplan免费挂下载(透视)新蛮王辅助(本来真的是有辅助辅助器)-哔哩哔哩新蛮王辅助是...
近日!aapoker脚本怎么用... 近日!aapoker脚本怎么用(透视)禅游指尖四川修改器(一贯有辅助工具)-哔哩哔哩所有人都在同一条...
此事迅速冲上热搜!aapoke... 此事迅速冲上热搜!aapoker免费透视脚本(透视)传送屋潮州激k辅助怎么打开(原来存在有辅助下载)...
近年来!wepoker永久免费... 近年来!wepoker永久免费脚本(透视)天天爱柳州有没有辅助器(真是真的有辅助安装)-哔哩哔哩1、...
昨日!wpk真吗(透视)万能游... 昨日!wpk真吗(透视)万能游戏辅助器脚本(真是是有辅助辅助器)-哔哩哔哩万能游戏辅助器脚本是不是有...