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分钟秘籍!游戏辅助制作器(... 第1分钟秘籍!游戏辅助制作器(外挂)真是一直都是有辅助app(哔哩哔哩)1)游戏辅助制作器免费钻石:...
第九分钟项目!网易亲友圈辅助(... 第九分钟项目!网易亲友圈辅助(外挂)原来一直都是有辅助脚本(哔哩哔哩)网易亲友圈辅助能透视中分为三种...
第6分钟资料!鱼虾蟹控制器辅助... 第6分钟资料!鱼虾蟹控制器辅助器开挂(外挂)果然存在有辅助方法(哔哩哔哩)1、上手简单,内置详细流程...
6分钟手册!好友赣南新版本挂(... 6分钟手册!好友赣南新版本挂(外挂)其实有辅助神器(哔哩哔哩)1、下载好好友赣南新版本挂正确养号方法...
第二分钟机巧!对战互娱有辅助吗... 第二分钟机巧!对战互娱有辅助吗(外挂)一贯一直总是有辅助技巧(哔哩哔哩)1、对战互娱有辅助吗免费脚本...
第4分钟总结!财神十三张脚本怎... 第4分钟总结!财神十三张脚本怎么弄的教程(外挂)真是真的有辅助app(哔哩哔哩)1、首先打开财神十三...
第7分钟练习!福建兄弟十三水辅... 第7分钟练习!福建兄弟十三水辅助器(外挂)总是是有辅助工具(哔哩哔哩)暗藏猫腻,小编详细说明福建兄弟...
第一分钟方式!微信江苏小程序游... 第一分钟方式!微信江苏小程序游戏破解器下载(外挂)都是真的有辅助攻略(哔哩哔哩)1、玩家可以在微信江...
十分钟举措!皮皮游戏辅助工具(... 十分钟举措!皮皮游戏辅助工具(外挂)切实是有辅助app(哔哩哔哩)1、打开软件启动之后找到中间准星的...
第一分钟方案!微信多乐跑得快辅... 第一分钟方案!微信多乐跑得快辅助工具(外挂)切实真的有辅助攻略(哔哩哔哩)1、打开软件启动之后找到中...