首页 > 其他分享 >angular响应式表单 setValue和pathValue的区别

angular响应式表单 setValue和pathValue的区别

时间:2023-03-12 10:48:30浏览次数:34  
标签:FormControl setValue firstName pathValue street profileForm new angular

<p>把表单控件分组</p>


<form [formGroup]="profileForm"  (ngSubmit)="onSubmit()">

    <label for="first-name">First Name: </label>
    <!-- 配合表单使用要用"formControlName",单个控件使用"formControl" -->
    <input id="first-name" type="text" formControlName="firstName">
  
    <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>

    <!-- buttton要设置为 type="submit" -->
    <button type="submit" class="btn btn-info" (click)="updateProfile()">使用patchValue更新部分数据</button>
    <button type="submit" class="btn btn-info" (click)="setValue()">使用setValue更新部分数据</button>

    <button type="submit" class="btn btn-primary" [disabled]="!profileForm.valid">Submit</button>
 </form>
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
  selector: 'app-profile-editor',
  templateUrl: './profile-editor.component.html',
  styleUrls: ['./profile-editor.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ProfileEditorComponent {
  profileForm = new FormGroup({
    firstName: new FormControl(''),
    lastName: new FormControl(''),
    address: new FormGroup({
      street: new FormControl(''),
      city: new FormControl(''),
      state: new FormControl(''),
      zip: new FormControl('')
    })
  })
  onSubmit() {
    console.warn(this.profileForm.value);
    console.warn(this.profileForm.get('firstName')!.value);
  }
  
  updateProfile() {
    // patchValue可以更新表单的部分数据
    this.profileForm.patchValue({
      firstName: 'Nancy',
      address: {
        street: '123 Drew Street'
      }
    });
  }
  setValue() {
    // setValue可以更新的数据,但必须要把所有的FormGroup中表单字段加上
    this.profileForm.setValue({
      firstName: 'firstName',
      lastName: 'lastName',
      address: {
        street: 'street',
        city: 'city',
        state: 'state',
        zip: 'zip',
      }
    })
  }
}

 

标签:FormControl,setValue,firstName,pathValue,street,profileForm,new,angular
From: https://www.cnblogs.com/zhaohui9527/p/17207710.html

相关文章

  • angular利用FormArray创建动态响应式表单
    FormArray 是 FormGroup 之外的另一个选择,用于管理任意数量的匿名控件。像 FormGroup 实例一样,你也可以往 FormArray 中动态插入和移除控件,并且 FormArray 实例的......
  • 「解题报告」CF1444D Rectangular Polyline
    这类型的题我们可以先找一些显然的必要条件,然后再去构造,很有可能就发现它是充分条件。考虑有什么必要条件:首先\(n=m\),要不然无法横纵首尾相连。其次所有横必定能划分......
  • angular onpush模式下优先使用markForCheck手动标记为脏属性并调用检测更新
    import{ChangeDetectorRef,Component,Input,ChangeDetectionStrategy,SimpleChanges}from'@angular/core';@Component({selector:'app-change-grandson',te......
  • Angular 独立组件入门
    Angular独立组件入门如果你正在学习​​Angular​​,那么你可能已经听说过独立组件(Component)。顾名思义,独立组件就是可以独立使用和管理的组件,它们能够被包含在其他组件中......
  • angular自定义验证
    内置验证器Validators.required:非空值验证器;Validators.maxLength(max):最大长度为max的验证器;Validators.minLength(min):最小长度为min的验证器;Validators.max......
  • angular ngIf和*ngIf的区别
    Angular中ngIf和*ngIf是等价的指令,它们的作用也是相同的,都用于根据条件控制元素的显示或隐藏。不同的是,*ngIf是结构型指令,它使用了Angular的模板语法糖,可以更加方便地控制......
  • angular在配置hosts后访问出现Invalid host header
    angular:localhost:4205hosts:127.0.0.1cas.woyujiezhen.comurl输入:cas.woyujiezhen.com:4205显示invalidhostheader解决办法:angular.json  "serve":{"optio......
  • 30道Angular经典面试题,背就完事了
    Angular中的路由是什么?它的作用是什么?Angular中的路由是一种用于管理应用程序导航和视图的机制。它允许您在应用程序中定义不同的路径,并将每个路径映射到一个特定的组件或模......
  • angularJs的作用域对象和控制器,依赖对象,依赖注入
    需要注意的是使用$scope,需要使用以下版本<scriptsrc="https://cdn.staticfile.org/angular.js/1.2.29/angular.min.js"></script>   代码部分<!DOCTYPE......
  • angular初步学习 踩坑一
    xxxx.ts  import{Component, Input,}from'@angular/core';exportinterfaceMenu{ title:string, acitie:boolean, id:number}//@component......