2017-11-16 4 views
0

私はAddress ---> Countryの関係を持っているとしましょう。アドレスフォームには国のコンボボックスがあります。国を選択すると、選択した国のメソッドが呼び出されます。IDを渡してコンボボックスを選択したときにメソッドを呼び出す方法は?

<select #countryCB class="form-control" id="field_country" name="country" [(ngModel)]="sponsorAgreement.country" required (change)="onChangeCountry(countryCB.value)"> 
    <option [ngValue]="countryOption.id === address.country?.id ? address.country : countryOption" 

このコードの問題点は、国のIDではなくインデックス(リストの第1国、第2国...)を渡すことです。

私はコードをデバッグしようとしましたが、そのIDはコンボ自体にはなく、私には得られないメカニズムがtrackByCountryIDであることに気付きました。あなたが見ることができるように、これは、生成されたもの何らIDではありません:

<option [ngValue]="countryOption.id === address.country?.id ? address.country : countryOption" 

それは(インデックス付き)次のHTMLコードを生成します:

<option value="2: Object" 
これとそう

<select class="form-control ng-untouched ng-pristine ng-valid" id="field_country" name="country" ng-reflect-name="country" ng-reflect-model="[object Object]"> 

<option value="0: null"></option> 
<option value="1: Object" ng-reflect-ng-value="[object Object]">Andorra</option> 
<option value="2: Object" ng-reflect-ng-value="[object Object]">United Arab Emirates</option> 
<option value="3: Object" ng-reflect-ng-value="[object Object]">Afghanistan</option> 

これに変更した場合:

<option [ngValue]="countryOption.id === address.country?.id ? address.country.id : countryOption.id" 

これは、(ID付き)以下のHTMLを生成します

<option value="1001: 1001" 

しかし、その後、背面のRESTサービスの中断は誰もがIDを取得する方法を知ってい

Bad Request: JSON parse error: Can not construct instance of io.quantixx.sponsor.domain.Country: no int/Int-argument constructor/factory method to deserialize from Number value (4); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of io.quantixx.sponsor.domain.Country: no int/Int-argument constructor/factory method to deserialize from Number value (4) 
at [Source: [email protected]; line: 8, column: 14] (through reference chain: io.quantixx.sponsor.domain.Address["country"]) 
2017-11-18 07:55:44.081 WARN 43780 --- [ XNIO-2 task-14] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not construct instance of io.quantixx.sponsor.domain.Country: no int/Int-argument constructor/factory method to deserialize from Number value (4); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of io.quantixx.sponsor.domain.Country: no int/Int-argument constructor/factory method to deserialize from Number value (4) 
at [Source: [email protected]; line: 8, column: 14] (through reference chain: io.quantixx.sponsor.domain.Address["country"]) 

を更新/作成? trackByCountryIDは関与していますか?

おかげ

答えて

0

私はまだtrackByが何を考え出したていないが、私は私のユースケースにはそれを必要としません。

私は、コンボボックスで要素を選択すると、選択したオブジェクトをメソッドに渡すために何をすべきかを知りました。 ngModelChangeを使用し、$eventを渡す必要があります。選択された要素は、自動的にメソッドに渡されます。

onChangeSponsor(sponsor: Sponsor): void { 
    // the sponsor parameter is set 
} 
:ここ

 <select class="form-control" id="field_sponsor" name="sponsor" [(ngModel)]="sponsorAgreement.sponsor" required (ngModelChange)="onChangeSponsor($event)"> 
      <option *ngIf="!editForm.value.sponsor" [ngValue]="null" selected></option> 
      <option [ngValue]="sponsorOption.id === sponsorAgreement.sponsor?.id ? sponsorAgreement.sponsor : sponsorOption" *ngFor="let sponsorOption of sponsors; trackBy: trackSponsorById">{{sponsorOption.organisation.name}} - {{sponsorOption.sponsorLevel.name}}</option> 
     </select> 

onChangeSponsor方法であり、

関連する問題