Angular 如果选中复选框,则使用其他字段值填充表单字段

Angular 如果选中复选框,则使用其他字段值填充表单字段,angular,typescript,angular5,angular-forms,Angular,Typescript,Angular5,Angular Forms,我有两部分表格——通讯地址和住址。如果选中复选框,则目标是将值从一个字段复制/传递到另一个字段。请参见下面的用例 如果选中复选框(与住宅地址相同),则使用住宅地址表单字段的值填充通信地址字段 如果选中复选框后居住地址中的任何表单字段发生更改,请将更改实时传递到通信中的字段 地址及 最后,如果复选框未选中,请清除通信地址中的表单值。下面是我试过的 组件技术 component.html 与住宅地址相同?) 上述代码并不完全符合预期,因为(i)取消选中复选框不会清除表单字段(ii)选中复选框后

我有两部分表格——通讯地址和住址。如果选中复选框,则目标是将值从一个字段复制/传递到另一个字段。请参见下面的用例

  • 如果选中复选框(与住宅地址相同),则使用住宅地址表单字段的值填充通信地址字段
  • 如果选中复选框后居住地址中的任何表单字段发生更改,请将更改实时传递到通信中的字段
    地址及
  • 最后,如果复选框未选中,请清除通信地址中的表单值。下面是我试过的
组件技术

component.html

与住宅地址相同?)
上述代码并不完全符合预期,因为(i)取消选中复选框不会清除表单字段(ii)选中复选框后,如果居住地址部分中的任何表单字段有任何更改,则通信表单字段不会更新以反映更改。 N:B:字段在一个表单组中


好心的导游

我简化了您的示例并使用:
-发送地址
-帐单地址

否则,我认为大致相同:)

TS

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormControl } from '@angular/forms';
import { EMPTY } from 'rxjs';
import { tap, distinctUntilChanged, switchMap, startWith } from 'rxjs/operators';

interface Address {
  street: string;
  city: string;
  country: string;
}

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  public isSameAddressControl: FormControl = new FormControl(false);

  public addresses: FormGroup = this.fb.group({
    sendingAddress: this.fb.group({
      street: '',
      city: '',
      country: ''
    }),
    billingAddress: this.fb.group({
      street: '',
      city: '',
      country: ''
    })
  });

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.isSameAddressControl
      .valueChanges
      .pipe(
        distinctUntilChanged(),
        switchMap(isSameAddress => {
          if (isSameAddress) {
            return this.addresses
              .get('sendingAddress')
              .valueChanges
              .pipe(
                // at the beginning fill the form with the current values
                startWith(this.addresses.get('sendingAddress').value),
                tap(value =>
                  // every time the sending address changes, update the billing address 
                  this.addresses
                    .get('billingAddress')
                    .setValue(value)
                )
              )
          } else {
            this.addresses
              .get('billingAddress')
              .reset();

            return EMPTY;
          }
        })
        // don't forget to unsubscribe when component's destroyed
      )
      .subscribe();
  }
}
HTML

<input type="checkbox" [formControl]="isSameAddressControl"> Same address for sending/billing

<form [formGroup]="addresses">
  <ng-container formGroupName="sendingAddress">
    Sending address<br>
    <input type="text" formControlName="street" placeholder="Street" autocomplete="off"><br>
    <input type="text" formControlName="city" placeholder="City" autocomplete="off"><br>
    <input type="text" formControlName="country" placeholder="Country" autocomplete="off"><br>
  </ng-container>

  <ng-container formGroupName="billingAddress">
    Billing address<br>
    <input type="text" formControlName="street" placeholder="Street" autocomplete="off"><br>
    <input type="text" formControlName="city" placeholder="City" autocomplete="off"><br>
    <input type="text" formControlName="country" placeholder="Country" autocomplete="off"><br>
  </ng-container>
</form>
发送/计费的相同地址
发送地址



帐单地址




这里有一个关于Stackblitz的工作示例:

因为它是一个反应式表单,所以您可以订阅表单值更改,并对您的指导进行必要的感谢。如何将此示例用于rxjs 5.5.2。我似乎无法让它代替管道工作。你必须为可观察对象链接调用(但你应该升级…),请你指导如何使用链而不是管道。谢谢你想知道为什么有人否决了我的答案。我很高兴得到一个解释:)
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormControl } from '@angular/forms';
import { EMPTY } from 'rxjs';
import { tap, distinctUntilChanged, switchMap, startWith } from 'rxjs/operators';

interface Address {
  street: string;
  city: string;
  country: string;
}

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  public isSameAddressControl: FormControl = new FormControl(false);

  public addresses: FormGroup = this.fb.group({
    sendingAddress: this.fb.group({
      street: '',
      city: '',
      country: ''
    }),
    billingAddress: this.fb.group({
      street: '',
      city: '',
      country: ''
    })
  });

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.isSameAddressControl
      .valueChanges
      .pipe(
        distinctUntilChanged(),
        switchMap(isSameAddress => {
          if (isSameAddress) {
            return this.addresses
              .get('sendingAddress')
              .valueChanges
              .pipe(
                // at the beginning fill the form with the current values
                startWith(this.addresses.get('sendingAddress').value),
                tap(value =>
                  // every time the sending address changes, update the billing address 
                  this.addresses
                    .get('billingAddress')
                    .setValue(value)
                )
              )
          } else {
            this.addresses
              .get('billingAddress')
              .reset();

            return EMPTY;
          }
        })
        // don't forget to unsubscribe when component's destroyed
      )
      .subscribe();
  }
}
<input type="checkbox" [formControl]="isSameAddressControl"> Same address for sending/billing

<form [formGroup]="addresses">
  <ng-container formGroupName="sendingAddress">
    Sending address<br>
    <input type="text" formControlName="street" placeholder="Street" autocomplete="off"><br>
    <input type="text" formControlName="city" placeholder="City" autocomplete="off"><br>
    <input type="text" formControlName="country" placeholder="Country" autocomplete="off"><br>
  </ng-container>

  <ng-container formGroupName="billingAddress">
    Billing address<br>
    <input type="text" formControlName="street" placeholder="Street" autocomplete="off"><br>
    <input type="text" formControlName="city" placeholder="City" autocomplete="off"><br>
    <input type="text" formControlName="country" placeholder="Country" autocomplete="off"><br>
  </ng-container>
</form>