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

相关内容

热门资讯

最终!来玩app 德州 辅助,... 最终!来玩app 德州 辅助,随意玩房卡代理有挂吗,一直是真的有辅助攻略(哔哩哔哩)1、随意玩房卡代...
今天下午!aa poker透视... 今天下午!aa poker透视软件,边锋干瞪眼脚本,竟然真的有辅助攻略(哔哩哔哩)1)边锋干瞪眼脚本...
突发!hhpoker外开挂靠谱... 突发!hhpoker外开挂靠谱吗,超级三加一辅助软件,果然有辅助app(哔哩哔哩)1、突发!hhpo...
记者获悉!wpk可以作必弊吗,... 记者获悉!wpk可以作必弊吗,奇迹陕西麻将辅助器,切实是有辅助方法(哔哩哔哩)1、实时奇迹陕西麻将辅...
长期以来!wepoker私人局... 长期以来!wepoker私人局透视教程,潮汕汇app透视软件,竟然真的是有辅助方法(哔哩哔哩)1、潮...
连日来!wepoker辅助器官... 连日来!wepoker辅助器官方,凑一桌游戏辅助器怎么安装,一直存在有辅助脚本(哔哩哔哩)运凑一桌游...
反观!poker辅助器免费安装... 反观!poker辅助器免费安装,逍遥辅助器,其实真的是有辅助软件(哔哩哔哩)1、金币登录送、破产送、...
一直以来!大菠萝手游辅助,微信... 一直以来!大菠萝手游辅助,微信小程序游戏修改器,一直有辅助技巧(哔哩哔哩)微信小程序游戏修改器脚本下...
日前!hhpoker软件安装包... 日前!hhpoker软件安装包,智星德州插件最新版本更新内容详解,都是真的有辅助方法(哔哩哔哩)1、...
有了最新消息!pokerwor... 有了最新消息!pokerworld软件,来来拼十免费辅助,好像有辅助神器(哔哩哔哩)来来拼十免费辅助...