【发布时间】:2019-07-06 21:14:16
【问题描述】:
我正在尝试将 Select 2 实施到我的 Angular 7 项目中。我按照所有程序从 github 实施。但它仍然无法正常工作。当我向其中提供一些静态数据时,它会起作用。它不填充来自网络服务器的数据。请分享你的想法。下面是我的代码
HTML代码:
<select2 [options]="areas" [settings]="{ width: '100%' }"></select2>
<div *ngFor="let ar of areas">
<p>{{ar.Area_Name}}</p>
</div>
组件代码:
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ObjectAreas } from './areasObject';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [
'./app.component.css',
'../../node_modules/bootstrap/dist/css/bootstrap.min.css'
]
})
export class AppComponent
{
apiURL: string = 'http://localhost/support/php/getAreas.php?areasno=0';
constructor(private httpClient: HttpClient) { }
areas: ObjectAreas;
public getContacts()
{
return this.httpClient.get(this.apiURL);
}
ngOnInit()
{
this.getContacts()
.subscribe((res:ObjectLoan)=>
{
this.areas = res;
});
}
}
模型类:
export class ObjectAreas
{
AreaSno: number;
Area_Code: string;
Area_Name: string;
}
我在控制台中收到此错误:
TypeError: Cannot read property 'toString' of undefined
【问题讨论】:
-
相反,ObjectLoan 使用 ObjectAreas,它应该数组遍历
-
我只使用 ObjectAreas。我在这里输入错误..
-
使其像 ObjectAreas[] 一样排列,并确保从 Web 服务中获取对象数组
-
我把它做成了数组。但没有得到。我正在从 Web 服务获取对象。我在下面的 html 中显示了它们 ..
-
所以除非你从服务中获取数组,否则它不会工作
标签: angular jquery-select2 angular7