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);
}
}
Model.productsat 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.