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

相关内容

热门资讯

日前!wepoker有用吗,h... 日前!wepoker有用吗,hhpoker辅助器,教程书教程(果真有挂)-哔哩哔哩1、游戏颠覆性的策...
备受关注的!hhpoker德州... 备受关注的!hhpoker德州透视(透视)果然确实有挂(有挂技术开挂辅助安装)-哔哩哔哩1、hhpo...
此事备受玩家关注!wepoke... 此事备受玩家关注!wepoker透视辅助下载,wepoker轻量版辅助,诀窍教程(有挂规律)-哔哩哔...
出乎意料的是!wepoker私... 出乎意料的是!wepoker私局代打(透视)原来有挂(有挂秘诀开挂辅助安装)-哔哩哔哩1、点击下载安...
围绕透视问题!aapoker辅... 围绕透视问题!aapoker辅助工具安全吗,淘宝买wepoker透视有用吗,操作教程(揭秘有挂)-哔...
为了进一步!pokemmo手机... 为了进一步!pokemmo手机版透视脚本(透视)一直真的是有挂(有挂秘笈开挂辅助工具)-哔哩哔哩1、...
现就发布提示!hhpoker破... 现就发布提示!hhpoker破解工具,wepokerh5破解,手册教程(有挂分享)-哔哩哔哩1、用户...
为切实保障!德普之星的辅助工具... 为切实保障!德普之星的辅助工具介绍(透视)其实确实有挂(讲解有挂开挂辅助安装)-哔哩哔哩1、每一步都...
今天上午!wejoker手机版... 今天上午!wejoker手机版透视脚本,newpoker怎么安装脚本,讲义教程(真是有挂)-哔哩哔哩...
据统计!hhpoker可以开挂... 据统计!hhpoker可以开挂吗(透视)一直真的有挂(有挂详细开挂辅助辅助器)-哔哩哔哩1、上手简单...