2017-08-29 75 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响应是即将或不来! –