我有一个NGFOR,里面有一个反应形式。这是代码:
<div class="element-list">
<form [formGroup]="formGroup" class="single-item" *ngFor="let p of points; let i = index">
<span>Lat: </span> <input formControlName="lat" type="text" value="{{p.lat}}">
<span>Lon: </span> <input formControlName="lon" type="text" value="{{p.lon}}">
<a (click)="updateData(i)">Submit</a>
</form>
</div>
这是TS侧
export class MapInfoBarComponent implements OnInit, OnDestroy
{
points: MapDetailModel[] = [];
isAlive = true;
formGroup: FormGroup;
constructor(
private mapService: MapService,
private fb: FormBuilder
){}
ngOnInit() {
this.mapService.pointsChanged
.takeWhile(() => this.isAlive)
.subscribe( evt => {
if(!!evt){
this.points = evt;
}
});
this.formGroup = this.fb.group({
lat: new FormControl(),
lon: new FormControl()
});
}
ngOnDestroy(): void {
this.isAlive = false;
}
updateData(id: number){
console.log(id + " > " + this.formGroup.value);
}
}
每当我点击
a
我从中得到了什么
this.formGroup.value
只是空值和空值。
问题
我的问题是,每当我单击一个按钮时,如何“知道”我指的是哪个窗体组?
this.points
有时会改变,从另一个组件推送新元素。
例如,我想更新第三个元素,并在
console.log
更新的值。
感谢您的帮助。谢谢。
我是如何解决这个问题的
感谢@ak.leimrey通过下面的链接。
我是这样解决的:
TS文件:
export class MapInfoBarComponent implements OnInit, OnDestroy
{
points: MapDetailModel[] = [];
isAlive = true;
survey: FormGroup;
items = false;
constructor(
private mapService: MapService,
private fb: FormBuilder
){}
ngOnInit() {
this.mapService.pointsChanged
.takeWhile(() => this.isAlive)
.subscribe( evt => {
if(!!evt){
this.points = evt;
if(!this.items){
this.survey = this.fb.group({
sections: this.fb.array([
this.initSection(0),
]),
});
this.items = true;
} else {
this.addSection(this.points.length - 1);
}
}
});
}
initSection(idx: number) {
return this.fb.group({
lat: [this.points[idx].lat],
lon: [this.points[idx].lon],
})
}
addSection(idx: number) {
const control = <FormArray>this.survey.get('sections');
control.push(this.initSection(idx));
}
getSections(form) {
//console.log(form.get('sections').controls);
return form.controls.sections.controls;
}
updateData(index: number){
const values = this.survey.value.sections[index] as MapDetailModel;
this.mapService.setChangePoint(values);
}
ngOnDestroy(): void {
this.isAlive = false;
}
}
HTML文件:
<div class="main-wrapper">
<div class="map-info-bar">
<div class="blocco-info">
{{lang.phrase('Coord')}}: {{lang.phrase('Lat')}} {{currentLatitude}} {{lang.phrase('Lon')}} {{currentLongitude}}
</div>
<div class="blocco-info">
<dx-select-box [items]="zoomLevels" [value]="scale" (onValueChanged)="onScaleChange($event.value)">
</dx-select-box>
</div>
</div>
<div class="element-list">
<form [formGroup]="survey" novalidate (ngSubmit)="onSubmit(survey)" *ngIf="items">
<div formArrayName="sections">
<div class="single-item" *ngFor="let section of getSections(survey); let i = index">
<div [formGroupName]="i">
<input type="text" formControlName="lat" class="frm-txt">
<input type="text" formControlName="lon" class="frm-txt">
<a (click)="updateData(i)">Update data</a>
</div>
</div>
</div>
</form>
</div>
</div>
班级:
export class MapDetailModel{
lat: number;
lon: number;
alt: number;
long?: number;
}