2017-08-29 81 views
-1

所以...我有網址(說... http://localhost:8080/oc-web/country),我的目標得到這個列表:填補JSON表

enter image description here

我要的是填充下拉與名單縣名,所以用戶可以選擇一個。

我也做了HTML下拉:enter image description here

我使用角2,我有服務,其中我設置從數據庫中獲取網址:enter image description here

我敢肯定,我有錯,所以需要幫助:d感謝

編輯:這是組件,如果能幫助.. enter image description here

+0

您的實際問題是什麼? –

+0

將無法​​正常工作......似乎我在這些代碼中有錯誤的代碼,或者我只是沒有很好地連接好事情。所以我發佈這個澄清和posible更正:D –

+0

我想這個plunker是你需要[鏈接](https://plnkr.co/edit/ywkAlWM4NimtNvhqx917?p=preview) –

回答

1

編輯:

看起來你的組件有問題。

首先,您必須觸發將檢索國家和分配響應的請求。 它會看起來像下面的(注意,我們指定可觀察):

@Component(...) 
export class CompanyTemplatePopupComponent { 

    countries; 

    ngOnInit(){ 
     countries = this.companyService.getCountries(); 
     // or whichever method you need to retrieve the list of countries 
    } 

} 

然後在你的模板(請注意使用async管):

<option *ngFor="let country of countries | async" [value]="country.id"> {{ country.name }} </option> 
+0

作出改變,但我仍然沒有得到下拉做填充。可能是其他的代碼也被誤解了嗎? –

+0

你能否提供你的組件的代碼?我認爲你的http響應鏈接到你的國家屬性的部分可能是問題 –

+0

我只是設置服務組件,所以你可以看看它:) –

0

的html代碼會

<select #countriesSelect class=custom-dropdown"> 
    <option selected disabled>Select Country</option> 
    <option *ngFor="let country of countries: [value]="country.id">{{country.name}}</option> 
</select> 

在ts代碼中,您必須在構造函數或ngOnInit()中獲取AllCountries()方法。 getAllCountries()是填充下拉列表的國家/地區的數據的方法

getAllCountries() { 
    this.getHttp().get(this.url) 
    .map((response) => { 
    this.countries = response; 
    console.log(response); // To check countries is coming or not! 
    }) 
} 
+1

首先檢查getAllCountries()方法是在構造函數還是ngOnInit上,然後檢查您的http響應是即將或不來! –