チェックボックスが動的に生成されるモデルをバインドして更新する正しい方法がわかりません。 (これは、Angoman 2ジェネレータを使用して最初に作成されたASP.NET Coreプロジェクトです)または、私がちょうど見つけたその問題の簡単なチェックボックス。ダイナミックチェックボックスリストにngModelをバインドする:角2/Typescript
以下は、機能とタイムスロットを持つコードを削除したものです。各施設は複数のタイムスロットを持つことができます。タイムスロットのチェックボックスのリストが表示されます。最初のデータは、1つのタイムスロットのみがチェックされるべきであることを示す。しかし、私が施設をロードすると、すべてのタイムスロットがチェックされます。彼らは私が期待したようにngModelと束縛されていません。
- 施設データに含まれるタイムスロットだけがすべてではなくチェックされるように修正するにはどうすればよいですか?
- ngModel timeslotidsプロパティにチェックされているものをバインドするにはどうすればよいですか?コンポーネントに配列プロパティを作成し、ngModelに頼る代わりにそのプロパティを操作する必要がありますか? (これを試しましたが、明らかにそうしていないので、以下のコードに戻っています)
- これはチェックする必要がないのでチェックしていないので、簡単なチェックボックス(haselectric)にバインドする際に問題があるようです。私はこのすべてを修正する輸入がないのですか?
コードの説明 私はインターフェイスにバインドするAPIから2つのオブジェクト(ファシリティとタイムスロット)を取得しています。彼らは大幅に簡略化のためにプロパティに減少している:
:これは、更新前に、単一の施設のためのJSONデータである[{"id":1,"timeslotname":"Daily"},{"id":2,"timeslotname":"Hourly"},{"id":4,"timeslotname":"Market"}]
:これはタイムスロットのためのJSONデータである
export interface IFacility {
id: number,
name: string,
haselectric: boolean,
timeslotids: number[],
timeslots: ITimeslot[]
}
export interface ITimeslot {
id: number,
timeslotname: string
}
{"id":2,"name":"Clements Building","haselectric":true,"timeslotids":[1],"timeslots":[{"id":1,"timeslotname":"Daily"}]}
施設を編集するためのコンポーネント(facility.component.html):
<form #form="ngForm" (ngSubmit)="submitForm()" *ngIf="formactive">
<input type="hidden" [(ngModel)]="updatedfacility.id" #id="ngModel" name="id" />
<div class="form-group">
<label for="name">Facility Name *</label>
<input type="text" class="form-control input-lg" [(ngModel)]="updatedfacility.name" #name="ngModel" name="name" placeholder="Facility Name *">
</div>
<div class="form-group">
<label for="exhibitspaces">Has Electric *</label>
<input type="checkbox" class="form-control input-lg" [(ngModel)]="updatedfacility.haselecric" #haselectric="ngModel" name="haselectric">
</div>
<div class="form-group">
<label for="timeslots">Select Timeslots Available for Rent *</label>
<div *ngIf="dbtimeslots" >
<span *ngFor="let timeslot of dbtimeslots" class="checkbox">
<label for="timeslot">
<input type="checkbox" [(ngModel)]="updatedfacility.timeslotids" value="{{timeslot.id}}" name="{{timeslot.id}}" [checked]="updatedfacility.timeslotids.indexOf(timeslot.id)" />{{timeslot.timeslotname}}
</label>
</span>
</div>
</div>
<button type="submit" class="btn btn-lg btn-default" [disabled]="form.invalid">Update</button>
</form>
コンポーネントのコード(facility.component.ts)
import { Component, OnInit, Input, Output, OnChanges, EventEmitter } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { ActivatedRoute, Router } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { FacilityService } from '../../../services/facility.service';
import { TimeslotService } from '../../../services/timeslot.service';
import { IFacility } from '../../../services/facility.interface';
import { ITimeslot } from '../../../services/timeslot.interface';
@Component({
template: require('./facility.component.html')
})
export class FacilityDetailsComponent {
facility: IFacility;
httpresponse: string;
successMessage: string;
errormessage: string;
showSuccess: boolean = true;
showError: boolean = true;
formactive: boolean = true;
dbtimeslots: ITimeslot[];
constructor(
private _route: ActivatedRoute,
private _router: Router,
private _facilityservice: FacilityService,
private _timeslotservice: TimeslotService
) {}
ngOnInit(): void {
this._timeslotservice.getTimeslots().subscribe(timeslots => this.dbtimeslots = timeslots, error => this.errormessage = <any>error);
let id = +this._route.snapshot.params['id'];
this._facilityservice.getFacility(id)
.subscribe(facility => this.facility = facility,
error => this.errormessage = <any>error);
}
submitForm() {
//update the facility through the service call
this._facilityservice.updateFacility(this.facility)
.subscribe(response => {
this.httpresponse = response;
console.log(this.httpresponse);
this.successMessage = "Facility updated!";
this.formactive = false;
setTimeout(() => this.formactive = true, 3);
this.showSuccess = false;
},
error => {
this.errormessage = <any>error;
this.showError = false;
});
}
}
P.S.私がtimeslotsのfacilityオブジェクトに2つのプロパティを持つのは、idのリストが更新のためにAPIに渡す方がはるかに簡単だからです。完全なモデルを渡すのではなく(タイムスロットは私がここにあるよりも大きく、データベースを更新する必要はありません)。それが達成される必要のあるものとは無関係なコメントを予約してください。
この質問が見つかりました...ngmodel binding with dynamic array of checkbox in angular2 これは試してもうまくいきませんでした:Get values from a dynamic checkbox list また、ローカルプロパティをチェックボックスの(変更した)バージョンで更新しようとしましたが、どちらも機能していないようです:Angular 2: Get Values of Multiple Checked Checkboxes
おかげで多くの。これを試してポストバックするつもりです。 – EHeine
これは、私が必要とする方向に私を指摘した。上記の私の元の記事を解決コードで更新しました。どうもありがとうございました! – EHeine
@EHeineあなたがそれを理解してうれしい。 –