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,你可以手动控制何时触发变更检测,以提高性能。

相关内容

热门资讯

六分钟规律!温州熟客麻将赢牌技... 六分钟规律!温州熟客麻将赢牌技巧,wepower伙牌(详细透视辅助器教程);温州熟客麻将赢牌技巧黑科...
7分钟教程!来几局app有没有... 7分钟教程!来几局app有没有外 挂,we poke软件免费版(详细透视辅助脚本教程);(需添加指定...
1分钟了解!同乡游有辅助软件吗... 1分钟了解!同乡游有辅助软件吗,来玩德州app辅助工具是真的(详细透视辅助黑科技教程);玩家必备必赢...
四分钟介绍!丽水都莱有辅助吗,... 四分钟介绍!丽水都莱有辅助吗,微扑克可以在软件内设置(详细透视辅助插件教程)是一款可以让一直输的玩家...
5分钟教学!一起宁德麻将钓蟹输... 5分钟教学!一起宁德麻将钓蟹输赢规律,德州透视辅助(详细透视辅助app教程);亲,其实确实真的有挂(...
四分钟技巧!新猴王金花有挂吗,... 【福星临门,好运相随】;四分钟技巧!新猴王金花有挂吗,微扑克系统是有问题(详细透视辅助挂教程);暗藏...
六分钟详情!欢乐贰柒拾有辅助吗... WePoke高级策略深度解析‌;六分钟详情!欢乐贰柒拾有辅助吗,德州ai辅助工具购买(详细透视辅助软...
四分钟规律!掌中乐游戏中心有辅... 四分钟规律!掌中乐游戏中心有辅助器吗,德扑ai操作(详细透视辅助app教程);最新版2024是一款经...
7分钟总结!福建大玩家隐藏胜率... 7分钟总结!福建大玩家隐藏胜率,扑克世界辅助(详细透视辅助脚本教程)关于福建大玩家隐藏胜率的基本介绍...
8分钟方法!微信小程序雀神广东... 这是一款非常优秀的微信小程序雀神广东麻将助赢神器 ia辅助检测软件,能够让你了解到微信小程序雀神广东...