Javascript JSON响应在数组中,但仍抛出一个错误;NgFor仅支持绑定到Iterables,如数组;
类型脚本文件Javascript JSON响应在数组中,但仍抛出一个错误;NgFor仅支持绑定到Iterables,如数组;,javascript,angular,typescript,angular2-services,Javascript,Angular,Typescript,Angular2 Services,类型脚本文件 export class CompanyComponent { apiService : APIService; data : any; private companyUrl = 'http://localhost:4000/api/company/'; constructor(apiService : APIService) { this.apiService = apiService; this.getCompanies(this.compa
export class CompanyComponent {
apiService : APIService;
data : any;
private companyUrl = 'http://localhost:4000/api/company/';
constructor(apiService : APIService) {
this.apiService = apiService;
this.getCompanies(this.companyUrl);
}
getCompanies(url: any){
this.data = this.apiService.GET(url).map((response :Response)=>
response.json()).subscribe((response)=>{
this.data = response;
console.log(this.data);
})
}
<div class="media">
<div class="media-left" >
<li class="media" *ngFor = "let comp of data" >
<label>{{comp}}</label>
</li>
</div>
</div>
响应数组
[
{"_id":"58f61a132d44240d085ca2fa","comapny_name":"Burslem
Spice","__v":1,"categories":["58f643382d44240d085ca2fb"]},<br>
{"_id":"590487964a45c56c0fa2911a","comapny_name":"Tiger
Bite","categories":[]}
]
[
{“_id”:“58f61a132d44240d085ca2fa”,“公司名称”:“Burslem”
香料“,”1,“类别“:[“58f643382d44240d085ca2fb”],
{“_id”:“590487964a45c56c0fa2911a”,“公司名称”:“老虎”
“咬”、“类别”:[]}
]
HTML文件
export class CompanyComponent {
apiService : APIService;
data : any;
private companyUrl = 'http://localhost:4000/api/company/';
constructor(apiService : APIService) {
this.apiService = apiService;
this.getCompanies(this.companyUrl);
}
getCompanies(url: any){
this.data = this.apiService.GET(url).map((response :Response)=>
response.json()).subscribe((response)=>{
this.data = response;
console.log(this.data);
})
}
<div class="media">
<div class="media-left" >
<li class="media" *ngFor = "let comp of data" >
<label>{{comp}}</label>
</li>
</div>
</div>
{{comp}}
上面的响应仍在数组中,但我得到以下错误:
错误原因:找不到类型为“object”的不同支持对象“[object object]”。NgFor仅支持绑定到Iterables
例如数组
数据:任何
说数据可以是任何东西,但*ngFor只接受Iterables。
将其更改为array
因为您正在加载异步,所以当第一次呈现dom时,数据
将是未定义的,因此angular会抱怨
export class CompanyComponent {
apiService : APIService;
data : Array; // or array
private companyUrl = 'http://localhost:4000/api/company/';
constructor(apiService : APIService) {
this.apiService = apiService;
this.getCompanies(this.companyUrl);
this.data = []
}
getCompanies(url: any){
this.apiService.GET(url).map((response :Response)=>
response.json()).subscribe((response)=>{
this.data = response;
console.log(this.data);
})
}
HTML
{{公司名称}
公司
和公司
是不同的@PriyeshKumar更新了更改。数据:数组不是必需的,它可以是数据:任意。棘手的部分是这个。我想数据=[]是的!但您应该将其定义为一些内置数据类型,以获得编辑器intellisense的好处,而typescript就是为Hi构建的。我也有类似的问题。我有这样的方法:getDropDownValues(){//这是用户权限的全部,isSA有某些权限等等。this.CompanyNames.permission().subscribe(permValues=>{if(permValues[this.selectedPerm]){this.dropDownList=permValues[this.selectedPerm];}}}我已将dropDownList声明为数组。DropDownList=[];但是,当我使用{{c.$value}}
时,我得到错误:尝试区分“[object]”时出错。在上面的代码中,只允许数组和iterables,我没有突出显示代码部分,因此:getDropDownValues(){//这是用户权限的全部,isSA拥有某些权限等等。this.CompanyNames.permission().subscribe(permValues=>{if(permValues[this.selectedPerm]){this.dropDownList=permValues[this.selectedPerm];});}