2017-11-14 9 views
0

バックエンドAPI呼び出しから国を選択するためのドロップダウンがあります。角度2のドロップダウンにあらかじめ設定された値

コード:

this.http.get(SERVER_URL+"/edit/getAllCountry") 
    .subscribe((res) => { 
     this.countries = res; 
    }); 

この意志は私に私のデータベースから国のリストを返します。

病気のようなHTMLでそれらを印刷すること:

billingPage.html

<select [(ngModel)]="country" (ngModelChange)="countryChanged(country)" > 
          <option *ngFor="let c of countries" [ngValue] = 'c'> {{c.countryName}} </option> 
         </select> 

、このbilling.htmlページに来る前に、私は国のために選択された値を持っている疑問。たとえば、 'USA'を選択し、ページが読み込まれたときにどのようにドロップダウンに読み込むことができますか?

ドロップダウンリストに 'USA'が選択されている必要があります。

+0

私はbilling.htmlページのドロップダウンリスト内の前のコントローラから解析された国を表示する必要がありません。 –

+0

アイデアは同じです。次のページのデフォルト値が選択されています。同じように設定されています。 –

答えて

1

それは我々が例以下のように、我々は値を保存し、別のAPIを呼び出して、選択してDB手段に保存したカスタム例.Onceある

selectedcountry:any; 

this.selectedcountry="USA"; 
this.http.get(SERVER_URL+"/edit/getAllCountry") 
.subscribe((res) => { 
    this.countries = res;   
}); 

Tsの

、これを試してみてください:

this.http.get(SERVER_URL+"/edit/getselectedCountry") 
.subscribe((data) => { 
    this.selectedcountry = data;   
}); 

billingPage.html

<select [(ngModel)]="selectedcountry" > 
    <option *ngFor="let c of countries" [selected]="selectedcountry==c.countryName" value="{{c.countryName}}"> {{c.countryName}} 
    </option> 

+0

は完全にty :) –

関連する問題