1

I have a form with many input fields that is dynamically created through binding:

<form method="post" (ngSubmit)="onSubmit()">
<div class="row">
  <div *ngFor="let p of Model.products" class="col-lg-2 col-md-4 col-sm-6" style="text-align:center">
    <p>{{p.name}}</p>
    <input name="stock:{{p.id}}" value="{{p.stock}}" type="text" class="form-control" autocomplete="off" onkeypress="return event.charCode >= 48 && event.charCode <= 57" />
    <img src="{{p.imgPath}}" style="max-width:120px" />
  </div>
</div>
<div style="text-align:right">
  <button type="submit" class="btn btn-outline-primary">Submit</button>
</div>
  </form>

How do I submit this form since I don't know the names of the inputs in advance. What should go in my onSubmit() event handler?

Here is the code of the component:

import { Component, Inject, OnInit } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AuthService } from "../services/auth.service";

@Component({
  selector: "admin-stocks",
  templateUrl: "stocks.component.html"
})

export class AdminStocksComponent implements OnInit{
  Model: ProductListModel;
  IsAdministrator: boolean = false;
  constructor(@Inject("BASE_URL") private baseUrl: string, private http: HttpClient,
                 private authService: AuthService) { }

  ngOnInit() {
    this.authService.isAdministrator().subscribe(res => this.IsAdministrator = res,
      error => console.error(error));

    var url = this.baseUrl + "api/ajax/GetProducts";
    this.http.get<ProductListModel>(url).subscribe(res => { console.log(res); this.Model = res; },
      error => console.error(error));
  }
  getPagesArray(n) {
    return new Array(n);
  }
  onSubmit(e) {
    console.log(e);

  }
}
3
  • Many useful informations are missing in the code: are you updating Model.products at some point? How are you supposed to call the server? A traditional url-encoded HTTP Post or an Ajax call? Try to describe the "shape" of the data you want to send. Without that information, we cannot know what the answer should look like. Commented Apr 15, 2020 at 11:10
  • I am updating the object Model in the onInit with an httpclient get. i want to call the server using httpclient with a post request. The data i want to send is the form itself that consists in a dictionary key value, where the key is the id of the product and the value is the stock of the product which is the value of the input of the form Commented Apr 15, 2020 at 11:17
  • i will update the question with the code of the component. Commented Apr 15, 2020 at 11:19

1 Answer 1

1

you just need to put the button with type submit inside the form tag so when the button clicked the submit event will trigger, and if you want the get access to form data you can create a refreance of ngForm directive

example

<form #form="ngForm" (ngSubmit)="onSubmit(form.value)">
    <input type="text" name="userName" ngModel>
    <button type="submit"> submit</button>
</form>

example of dynamic form controls and set the inital value by ngModel

<form #form="ngForm" (ngSubmit)="onSubmit(form.value)">
 <input 
    *ngFor="let p of controls" 
     type="text" 
     [name]="p.id" 
     [ngModel]="p.value">
  <button type="submit"> submit</button>
</form>

demo 🚀

check this 👉 Angular Form ✨ it's very helpful

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

6 Comments

if i add ngModel to the field its bound value disappears
you can set the initail value like this [ngModel]="p.value" and everything will work fine
i see now it works, but what kind of object i get in onSubmit. Is it formdata? Can I post it to a controller and then receive it as form data?
the value you get is base of the form inputs any and the fomm controls keys ill be base of the name property
How do I loop through the form.value in order to get a key-pair dictionary. i tried for (var key in f.value) { console.log(key); } this gives me the keys but not the values
|

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.