I have a json array of objects that I bind to html form and mat-expansion-panel
view. I'm trying to get changed value of name
input field but in any case it returns the initial value. e.g. If I change name1
to name111
and press submit button I see name1
in the alert window. If use this.myForm.controls["name"].value
it returns last changed value but it is incorrect because I could press save button of another item.
How to get all values of input fields of the current item on submit event?(include changed values)
https://stackblitz.com/edit/angular-3xjnjv?file=src%2Fapp%2Fapp.component.ts
example:
1)I get
id1 | name = name1 | save button1
id2 | name = name2 | save button2
id3 | name = name3 | save button3
2) I change
id1 | name = changedname1 | save button1
id2 | name = changedname2 | save button2
id3 | name = changedname3 | save button3
3) I press save button2
4)I see item: changedname2
onSubmit() {
window.alert("item: " + this.selItem.name); // item: changedname2
}
app.component.html
<form [formGroup]="myForm" (ngSubmit)="onSubmit()">
<mat-expansion-panel [expanded]="isFirst" *ngFor="let item of items; let isFirst = first; let i = index;">
<mat-expansion-panel-header>
<mat-panel-title>
<h3>{{item.id}}</h3>
</mat-panel-title>
<mat-panel-description>
This is a summary of the content
</mat-panel-description>
</mat-expansion-panel-header>
<div class="row">
<div>
<input matInput formControlName="id" value="{{item.id}}" placeholder="{{id}}" type="text">
</div>
</div>
<div class="row">
<div>
<input matInput formControlName="name" value="{{item.name}}" placeholder="{{name}}">
</div>
</div>
<button (click)="setItem(item)" mat-raised-button >{{updateItem}}</button>
</mat-expansion-panel>
</form>
app.component.ts
import { Component } from '@angular/core';
import { ChangeDetectionStrategy, OnInit, ViewChild } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { map } from 'rxjs/operators';
import { MatSort, Sort } from '@angular/material';
import { MatPaginator, PageEvent } from '@angular/material';
import { fromMatSort, sortRows } from './datasource-utils';
import { fromMatPaginator, paginateRows } from './datasource-utils';
import { FormBuilder, FormGroup, FormArray , Validators } from '@angular/forms';
export interface PeriodicElement {
id: string;
name: string;
}
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
updateItem = 'Save';
myForm: FormGroup;
items: any;
selItem: any;
constructor(private formBuilder: FormBuilder) { }
onSubmit() {
window.alert("item: " + this.selItem.name);
window.alert(this.myForm.controls["name"].value);
// ...
}
ngOnInit() {
this.myForm = this.formBuilder.group({
id: [''],
name: ['']
});
const ELEMENT_DATA: PeriodicElement[] = [{"id":"id1",
"name":"name1"},
{"id":"id2",
"name":"name2"},
{"id":"id3",
"name":"name3"}
//,...
];
this.items = ELEMENT_DATA;
}
setItem(item) {
this.selItem = item;
}
}