アプリケーションでAngular 2モデル駆動型フォームを使用しようとしていますが、ネストされたオブジェクト(null値を持つ)がある場合、期待どおりに機能させることができません。
これが私のコードです:
person.model.ts (これはネストされたオブジェクトとしてアドレスを持つ Person モデル オブジェクトです)
import {Address} from './address.model';
export class Person{
personId: string;
name: string
age: number;
address: Address;
}
address.model.ts
export class Address{
addressId: string;
street: string
city: string;
state: string;
zip: string
}
person.component.ts
@Component( {
selector: 'app-person',
templateUrl: 'person.component.html'
})
export class PersonComponent implements OnInit {
personForm: FormGroup;
person: Person;
constructor( private someService: PersonService, private formBuilder: FormBuilder) {}
ngOnInit(){
this.someService.getPersonCall( personId)
.subscribe ( person => {
this.person = person;
this.buildForm();
}
};
buildForm(): void {
this.personForm = this.formBuilder.group ({
'name': [this.person.name, [Validator.required]],
'age': [this.person.age],
'street': [this.person.address.streetOne],
'city': [this.person.address.streetOne],
'state': [this.person.address.state],
'zip': [this.person.address.zip],
});
this.registerForChanges();
}
registerForChanges(): void {
this.personForm.get('name').valueChanges.subscribe(value => this.person.name=value);
this.personForm.get('age').valueChanges.subscribe(value => this.person.age=value);
this.personForm.get('street').valueChanges.subscribe(value => this.person.address.streetOne=value);
this.personForm.get('city').valueChanges.subscribe(value => this.person.address.city=value);
this.personForm.get('state').valueChanges.subscribe(value => this.person.address.state=value);
this.personForm.get('zip').valueChanges.subscribe(value => this.person.address.zip=value);
}
onSubmit() {
this.someService.update(this.person).subscribe( response =>
this.person = response);
}
ここに私の person.component.html があります
<form *ngIf="person" (ngSubmit)="onSubmit()" [formGroup]="personForm"
novalidate>
<div class="col-md-6">
<div class="form-group">
<label for="nameId">Name</label>
<input type="text" class="form-control" formControlName="name" id="nameId"/>
</div>
<div class="form-group">
<label for="ageId">Age</label>
<input type="number" class="form-control" formControlName="age" id="ageId"/>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="streetId">Street</label>
<input type="text" class="form-control" formControlName="street" id="streetId"/>
</div>
<div class="form-group">
<label for="cityId">City</label>
<input type="text" class="form-control" formControlName="city" id="cityId"/>
</div>
<div class="form-group">
<label for="stateId">State</label>
<input type="text" class="form-control" formControlName="state" id="stateId"/>
</div>
<div class="form-group">
<label for="zipId">Zip</label>
<input type="text" class="form-control" formControlName="zip" id="zipId"/>
</div>
</div>
<button type="submit" [disabled]="!personForm.valid">Save </button>
</form>
サービス呼び出しから入力した人物オブジェクトを更新しようとしていますが、人物オブジェクトを取得すると、人物オブジェクトの address プロパティに null 値があり、buildForm 関数のコードが壊れています。
私は他のいくつかの方法を試しましたが、それを機能させることができませんでした
バージョン #2
buildForm(): void {
if ( !this.person.address ) {
this.personForm = this.formBuilder.group ({
'name': [this.person.name, [Validator.required]],
'age': [this.person.age],
'street': [''],
'city': [''],
'state': [''],
'zip': [''],
});
} else {
this.personForm = this.formBuilder.group ({
'name': [this.person.name, [Validator.required]],
'age': [this.person.age],
'street': [this.person.address.streetOne],
'city': [this.person.address.streetOne],
'state': [this.person.address.state],
'zip': [this.person.address.zip],
});
}
this.registerForChanges();
}
この変更により、エラーなしでフォームをレンダリングできましたが、住所フィールドを更新しようとすると、registerForChanges 関数で失敗しました。
バージョン #3
registerForChanges(): void {
if (! person.address) {
person.address = new Address();
this.personForm.get('name').valueChanges.subscribe(value => this.person.name=value);
this.personForm.get('age').valueChanges.subscribe(value => this.person.age=value);
this.personForm.get('street').valueChanges.subscribe(value => this.person.address.streetOne=value);
this.personForm.get('city').valueChanges.subscribe(value => this.person.address.city=value);
this.personForm.get('state').valueChanges.subscribe(value => this.person.address.state=value);
this.personForm.get('zip').valueChanges.subscribe(value => this.person.address.zip=value);
}
この変更の後、住所フィールドを変更せずにフォームを保存すると、空のレコードが住所テーブルに追加されてしまいます。
関数のアドレス プロパティが null でない場合、このコードは問題なく動作します。
誰かがこのコードを機能させるのを手伝ってくれますか