• patchValue:设置表单控件的值(可以设置全部,也可以设置其中某一个,其他不受影响)
    • setValue:设置表单控件的值 (设置全部,不能排除任何一个)
    • valueChanges:当表单控件的值发生变化时被触发的事件
    • reset:表单内容置空 ```typescript import { FormGroup, FormControl } from ‘@angular/forms’; import { Component, OnInit } from ‘@angular/core’;

    @Component({ selector: ‘app-method’, templateUrl: ‘./method.component.html’, styleUrls: [‘./method.component.scss’] })

    export class MethodComponent implements OnInit { form: FormGroup = new FormGroup({ firstName: new FormControl(), lastName: new FormControl(), })

    1. // 设置表单控件的值(可以设置全部,也可以设置其中某一个,其他不受影响)
    2. onPatchValue() {
    3. this.form.patchValue({
    4. firstName: 'test',
    5. })
    6. }
    7. // 设置表单控件的值 (设置全部,不能排除任何一个)
    8. onSetValue() {
    9. // 只修改一个 firstName 不行
    10. this.form.patchValue({
    11. firstName: 'test',
    12. lastName: 'test'
    13. })
    14. }
    15. onReset() {
    16. this.form.reset()
    17. }
    18. onSubmit() {
    19. }
    20. constructor() { }
    21. ngOnInit(): void {
    22. // 当表单控件的值发生变化时被触发的事件
    23. this.form.get('lastName')?.valueChanges.subscribe((value) => {
    24. console.log(value)
    25. })
    26. }

    }

    1. ```typescript
    2. <form [formGroup]="form" (submit)="onSubmit()">
    3. <input type="text" formControlName="firstName">
    4. <input type="text" formControlName="lastName">
    5. <button (click)="onPatchValue()">patchValue</button>
    6. <button (click)="onSetValue()">setValue</button>
    7. <button (click)="onReset()">reset</button>
    8. <button>提交</button>
    9. </form>