-1

I am getting the form values as undefined upon form submit.


   @ViewChild('someForm', { static: true }) form: ElementRef<HTMLFormElement>;
   public apiReponse: JsonResponse;
   this.route.queryParams
           .pipe(
               switchMap((params) =>
                   this.authService.getToken$(
                     param1, param2
                   )
               ),
               first(),
               takeUntil(this.ngUnsubscribe$)
           )
          .subscribe({
               next: (returnJson) => {
                   const responseObj = JSON.parse(returnJson);
                   this.apiReponse = responseObj;
                   console.log('response', this.apiReponse);
                   console.log('this form', this.form.nativeElement);
                   this.form.nativeElement.submit();
               },
               error: () => {
                   console.log('Some error ');
               },
           });

and my form is

  <form
   #someForm
   ngNoForm
   method="post"
   enctype="application/x-www-form-urlencoded"
    >
   <input type="hidden" id="token" name="token" [value]="apiReponse?.token" />
    </form>
af_khan
  • 1,030
  • 4
  • 25
  • 49

1 Answers1

0

I have faced this kind of issue earlier and adding the submit() in setTime out worked.

subscribe({
    next: (returnJson) => {
      const responseObj = JSON.parse(returnJson);
      this.apiResponse = responseObj;
      console.log('response', this.apiResponse);
      console.log('this form', this.form.nativeElement);
      setTimeout(() => {
        this.form.nativeElement.submit();
      }, 100); 
    },
Sai Manoj
  • 3,809
  • 1
  • 14
  • 35