问题
I have an Array of Student
Objects. The interface looks something like this:
interface Student {
title: string;
firstName: string;
lastName: string;
dob: string;
age: number;
}
I want to edit title
, firstName
& lastName
.
The form will have array of student objects. There is already some data in objects fetched from db.
title
is a dropdown, firstName
and lastName
are textboxes.
There will be a save button, on click of which the values of the form should be bundled and sent through Student[]
to typescript.
How can I achieve this?
回答1:
You could use a Reactive Form for this.
First get your data and generate a FormGroup
accordingly. I'm doing that using FormBuilder
in the ngOnInit
method.
Whatever you get from the API can be mapped as a FormArray
of FormGroup
(s).
Now in the template, you would just have to use the formControlName
directive on a select list to auto-populate it with the API data for the title
property.
Give this a try:
import { Component } from "@angular/core";
import { FormGroup, FormArray, FormBuilder, FormControl } from "@angular/forms";
import { HttpClient } from "@angular/common/http";
interface Student {
title: string;
firstName: string;
lastName: string;
dob: string;
age: number;
}
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
name = "Angular";
form: FormGroup;
constructor(private http: HttpClient, private fb: FormBuilder) {}
ngOnInit() {
this.http.get("/assets/data.json").subscribe((students: Array<Student>) => {
this.form = this.fb.group({
students: this.fb.array(
students.map(student =>
this.fb.group({
title: [student.title],
firstName: [student.firstName],
lastName: [student.lastName],
dob: [student.dob],
age: [student.age]
})
)
)
});
});
}
onSubmit() {
console.log("Form Value: ", this.form.value);
}
}
And in the template:
<form
*ngIf="form"
[formGroup]="form"
(submit)="onSubmit()">
<div
formArrayName="students"
*ngFor="let studentFormGroup of form.controls['students'].controls; let i = index;">
<div [formGroupName]="i">
<label for="title">Title</label>
<select name="title" id="title" formControlName="title">
<option value="" disabled>Select</option>
<option value="Mr.">Mr.</option>
<option value="Mrs.">Mrs.</option>
<option value="Ms.">Ms.</option>
</select>
<br>
<label for="firstName">First Name</label>
<input type="text" id="firstName" formControlName="firstName">
<br>
<label for="lastName">Last Name</label>
<input type="text" id="lastName" formControlName="lastName">
<br>
<label for="dob">DOB</label>
<input type="text" id="dob" formControlName="dob">
<br>
<label for="age">Age</label>
<input type="text" id="age" formControlName="age">
<br>
<hr>
<br>
</div>
</div>
<button type="submit">Submit</button>
</form>
Here's a Working Code Sample for your ref.
回答2:
You can use the *ngFor
property on an element like <li>
, <div>
or <ng-container>
to display each student like this:
<ul>
<li *ngFor="let student of students">
{{ student.firstName }}
</li>
</ul>
Because of Angular's two way binding you can put html from input in there bound to the student's properties.
Sources:
https://angular.io/guide/displaying-data
https://angular.io/api/core/Input
https://docs.angularjs.org/api/ng/directive/select
来源:https://stackoverflow.com/questions/58655391/how-do-i-bind-values-of-edit-form-with-array-of-object-in-angular-5