首页 > 其他分享 >angular FormArray 中嵌套 FormGroup 问题解决

angular FormArray 中嵌套 FormGroup 问题解决

时间:2022-11-28 20:00:37浏览次数:33  
标签:group FormArray fb profileForm FormGroup angular aliases

官方例子里说了FormArray可以嵌套group或者array,但只给了control的实例,这里记录一下嵌套group

ts文件:

import { Component } from '@angular/core'; import { FormBuilder } from '@angular/forms'; import { Validators } from '@angular/forms'; import { FormArray } from '@angular/forms';
@Component({   selector: 'app-profile-editor',   templateUrl: './profile-editor.component.html',   styleUrls: ['./profile-editor.component.css'], }) export class ProfileEditorComponent {   profileForm = this.fb.group({     firstName: ['', Validators.required],     lastName: [''],     address: this.fb.group({       street: [''],       city: [''],       state: [''],       zip: [''],     }),     aliases: this.fb.array([]),   });
  get aliases() {     return this.profileForm.get('aliases') as FormArray;   }
  constructor(private fb: FormBuilder) {}
  updateProfile() {     this.profileForm.patchValue({       firstName: 'Nancy',       address: {         street: '123 Drew Street',       },     });   }
  addAlias() {     const temp = this.fb.group({       street: ['2'],       city: ['2'],     });     this.aliases.push(temp);   }
  onSubmit() {     // TODO: Use EventEmitter with form value     console.warn(this.profileForm.value);   } }
/* Copyright Google LLC. All Rights Reserved. Use of this source code is governed by an MIT-style license that can be found in the LICENSE file at https://angular.io/license */   html文件 <form [formGroup]="profileForm" (ngSubmit)="onSubmit()">   <label for="first-name">First Name: </label>   <input id="first-name" type="text" formControlName="firstName" required />
  <label for="last-name">Last Name: </label>   <input id="last-name" type="text" formControlName="lastName" />
  <div formGroupName="address">     <h2>Address</h2>
    <label for="street">Street: </label>     <input id="street" type="text" formControlName="street" />
    <label for="city">City: </label>     <input id="city" type="text" formControlName="city" />
    <label for="state">State: </label>     <input id="state" type="text" formControlName="state" />
    <label for="zip">Zip Code: </label>     <input id="zip" type="text" formControlName="zip" />   </div>
  <div formArrayName="aliases">     <h2>Aliases</h2>     <button type="button" (click)="addAlias()">+ Add another alias</button>
    <div       *ngFor="let alias of aliases.controls; let i = index"       [formGroupName]="i"     >       <label for="street-{{ i }}">Street-{{ i + 1 }}: </label>       <input id="street-{{ i }}" type="text" formControlName="street" />       <label for="city-{{ i }}">City-{{ i + 1 }}: </label>       <input id="city-{{ i }}" type="text" formControlName="city" />     </div>   </div>
  <p>Complete the form to enable button.</p>   <button type="submit" [disabled]="!profileForm.valid">Submit</button> </form>
<hr />
<p>Form Value: {{ profileForm.value | json }}</p>
<p>Form Status: {{ profileForm.status }}</p>
<button type="button" (click)="updateProfile()">Update Profile</button>
<!--  Copyright Google LLC. All Rights Reserved. Use of this source code is governed by an MIT-style license that can be found in the LICENSE file at https://angular.io/license -->

标签:group,FormArray,fb,profileForm,FormGroup,angular,aliases
From: https://www.cnblogs.com/xjbkw/p/16933439.html

相关文章

  • Angular2文档学习的知识点摘要——Angular模块(NgModule)
    目录​​目录​​​​前言​​​Angular模块化​​​AppModule-应用的根模块​​​​在maints中引导​​​​声明指令和组件​​​​服务提供商​​​​导入支持性模块​......
  • AngularJS自学之路——知识点记录(一)
    AngularJS(以下知识点摘抄自《AngularJS权威教程》一书。)1、ng-app指令标记了AngularJS脚本的作用域;2、AngularJS应用引导过程有3个重要点:1)注入器(injector)将用于创建此应用程......
  • Angular2文档学习的知识点摘要——显示数据、用户输入、表单
    目录​​目录​​​显示数据​​​使用插值表达式显示组件属性​​​用户输入​​​模板引用变量​​​​按键事件过滤​​​表单​​​模板驱动的表单​​​案例angular-fo......
  • Angular2文档学习的知识点摘要——依赖注入
    目录​​目录​​​依赖注入(DI)​​​为什么需要依赖注入?​​​Angular依赖注入​​​配置注入器​​​​在NgModule中注册提供商​​​​在组件中注册提供商​​​​该用......
  • Ionic +Angular 打包后Http不能访问
    针对使用Capacitor打包的可在项目根目录下找到capacitor.config.ts文件在内容增加server:{   'cleartext':true  }如下:  再重新打包就可以了针对......
  • 谷歌发布 AngularJS 1.0,允许扩展HTML语法
    谷歌2012年06月15日发布了一个全新的Web模板——AngularJS1.0。谷歌称,AngularJS可以让你扩展HTML的语法,以便清晰、简洁地表示应用程序中的组件,并允许将标准的HTML作为你的......
  • 【Angular】162-Angular6入门项目(三)
    本文目录一、项目起步二、编写路由组件三、编写页面组件1.编写单一组件2.模拟数据3.编写主从组件四、编写服务1.为什么需要服务2.编写服务五、引入RxJS1.关于RxJS2.引入RxJS......
  • Angular样式隔离(style isolation)及选择器(:host, :host-context, ::ng-deep)的使用
    1.Angular样式隔离Angular样式隔离的好处最最要的一条就是CSS的可维护性。当没有样式隔离时,我们创建一个组件并添加样式后,可能会影响到其他的组件样式,而且很有可能查找不......
  • angularjs解决播放语音出错的问题
    首先是看了github上的这个​​https://github.com/angular/angular.js/issues/1352​​,知道了语音播放不了了,下面是正确的播放代码首先添加过滤器app.filter("trustUrl",['......
  • angularjs解析html字符串
    参考链接​​https://docs.angularjs.org/api/ngSanitize​​首先下载angular-sanitize.min.js,下载地址:​​https://github.com/angular/bower-angular-sanitize​​添加解......