Angular 9:将url中的参数传递给表单参数

时间:2020-05-28 15:14:17

标签: angular

我需要传递url参数以将后端一起形成参数。我从URL这样获得令牌参数:

private initForm(): void {
    this.route.queryParams.subscribe(params => {
      const token = params['token']; // get the token from url
      console.log(token)
    });

    this.formGroup = this.formBuilder.group({
      password: ['', Validators.required],
      password_confirmation: ['', Validators.required],
      reminder: ['', Validators.required]
    });
  }

在我的onSubmit方法上,我有它:

public onSubmit(event: any): void {

    if (this.formGroup.invalid) {
      return this.formGroup.markAllAsTouched();
    }

    const { password, password_confirmation, reminder } = this.formGroup.value;

    const subscription = this.updatePasswordComponentService
      .updatePassword(password, password_confirmation, reminder)
      .subscribe(result => {
        console.log(result);
        subscription.unsubscribe();
      });
  }

updatePassword方法

public updatePassword(password: string, password_confirmation: string, reminder: string): Observable<any> {
    return this.httpClient.post(url, {
      password, password_confirmation, reminder
    });
  }

我需要传递这样的参数:

public updatePassword(password: string, password_confirmation: string, reminder: string, token: string): Observable<any> {
    return this.httpClient.post(url, {
      password, password_confirmation, reminder, token
    });
  }

如何将tokenpasswordpassword_confirmation参数一起包含在reminder中?

1 个答案:

答案 0 :(得分:1)

您总是可以像这样从路由快照获取queryParams

const total = 100;
let otherNumbers = [100, 200];

const otherNumbersMatches = otherNumbers.find(num => num === total);

if ((total >= 52001 && total <= 52006) || otherNumbersMatches) {
  // do something
  console.log('Matches')
}

并将其作为请求正文

传递