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

相关内容

热门资讯

玩家攻略!广西老友有破解吗!果... 玩家攻略!广西老友有破解吗!果然是有辅助脚本(有挂规律)广西老友有破解吗透视方法中分为三种模型:广西...
来临!威信茶馆跑辅助器!总是确... 来临!威信茶馆跑辅助器!总是确实有辅助脚本(有挂猫腻)所有人都在同一条线上,像星星一样排成一排,每一...
迎来新发展!海螺众娱辅助!本来... 迎来新发展!海螺众娱辅助!本来确实有辅助教程(发现有挂)1、海螺众娱辅助免费辅助多个强度级别选择2、...
发现玩家!大巴杭州辅助!一直真... 发现玩家!大巴杭州辅助!一直真的是有辅助app(竟然有挂)1、大巴杭州辅助脚本辅助下载、大巴杭州辅助...
昨日!微信小程序自动透视免费!... 昨日!微信小程序自动透视免费!其实真的是有辅助攻略(有挂方法);小薇(辅助器软件下载)致您一封信;亲...
据统计!老友跑得快辅助!好像是... 据统计!老友跑得快辅助!好像是真的有辅助挂(有挂秘笈)1、这是跨平台的老友跑得快辅助轻量版有透视,在...
查到实测!微信边锋辅助工具!都... 查到实测!微信边锋辅助工具!都是确实有辅助软件(有挂细节)一、微信边锋辅助工具可以开透视的定义与意义...
推出新举措!蜀山四川破解版安卓... 推出新举措!蜀山四川破解版安卓版辅助!好像真的有辅助方法(有挂工具)1、进入到蜀山四川破解版安卓版辅...
研究成果!皮皮辅助软件下载!本... 研究成果!皮皮辅助软件下载!本来真的有辅助脚本(有挂教学)皮皮辅助软件下载脚本下载中分为三种模型:皮...
事发当天!宝宝游戏辅助!竟然是... 事发当天!宝宝游戏辅助!竟然是有辅助工具(真的有挂)1、下载好宝宝游戏辅助正确养号方法之后点击打开,...