Dok Nabaku
Dok Nabaku

Reputation: 83

Angular 9: passing param from url together a form params

I need to pass a url param to backend together form params. I get the token params from URL like this:

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]
    });
  }

On my onSubmit method, I have it:

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();
      });
  }

The updatePassword method

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

I need to pass params like that:

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

How can I include the token together password, password_confirmation and reminder params?

Upvotes: 0

Views: 49

Answers (1)

shumih
shumih

Reputation: 457

You always can get queryParams from route snapshot like this

this.updatePasswordComponentService.updatePassword(
    password, 
    password_confirmation, 
    reminder, 
    this.route.snapshot.queryParams.token)

and pass it as request body

Upvotes: 1

Related Questions