3

In my angular 5 application I need to submit a hidden form programmatically after an Http GET call, but when the form is submitted the three variables I am sending seems empty (but they don't).

In my component I have :

@ViewChild('testForm') testFormEl: any;


this.ticketService.postTicketWithCreditPayment(param)
    .subscribe(res => {
        this.paymentUrl = res.url;
        this.paymentXml = res.xml;
        this.paymentHash = res.hash;

        console.log(this.paymentUrl) <-- this log make me see the correct paymentUrl 

        this.testFormEl.nativeElement.submit();
     })
    }

And in my html I have the form with 3 hidden fields

<form #testForm name="form1" action="https://...url..." method="post">
    <input type="hidden" name="url" [value]="paymentUrl">
    <input type="hidden" name="xml" [(ngModel)]="paymentXml">
    <input type="hidden" name="hash" [(ngModel)]="paymentHash">
</form>

I try binding with [value] and [(ngModel)] but the form submitted is always this: enter image description here

Is probably related to : stackoverflow

6
  • If you downvote please explain me why!!! Commented Mar 8, 2018 at 9:25
  • 1
    (I didn't down vote) I think you shoud use reactive form provided with angular. You can find a simple tutorial here : coursetro.com/posts/code/66/Angular-4-Reactive-Forms-Tutorial Commented Mar 8, 2018 at 9:28
  • i think the values populated inside ngModel only as response of user changes event, so if object was initially empty you need to care about set its values somehow Commented Mar 8, 2018 at 9:36
  • How can I submit the reactive form in the component? @ibenjelloun Commented Mar 8, 2018 at 9:44
  • 1- Create your form using FromBuilder. 2- Link it to the template. 3- When click on submit use HttpClient's appropriate http function to send form.values to the backend. Commented Mar 8, 2018 at 9:50

1 Answer 1

6

try to rewrite your HTML to:

<form #testForm name="form1" action="https://...url..." method="post">
  <input type="hidden" name="url" [ngModel]="paymentUrl">
  <input type="hidden" name="xml" [ngModel]="paymentXml">
  <input type="hidden" name="hash" [ngModel]="paymentHash">
</form>

[value] instead of [ngModel] will also work here: one way binding from the controller.

in your TS file:

// get reference to the form
@ViewChild('testForm') testFormEl;
...

in your subscribe method, wrap form submit with setTimeout(), so submit will be moved to the next tick:

setTimeout(_ => this.testFormEl.nativeElement.submit());

It should work now.

I created a stackblitz (https://angular-f7lg76.stackblitz.io), in which I removed method and action attributes from the form element, so it executes GET to its self, and you can see sent data in the URL address bar.

Stackblitz source: https://stackblitz.com/edit/angular-f7lg76?file=app%2Fapp.component.ts

Sign up to request clarification or add additional context in comments.

2 Comments

Properly explained and the setTimeout is the best solution
@NileshSutar. I am glad that it saved your time :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.