我正在尝试将Select 2实现到我的Angular 7项目中。我按照从Github实现的所有过程进行了操作。但是它仍然没有工作。当我向其提供一些静态数据时,它可以正常工作。但是它无法从Web服务器填充数据。请分享您的想法。以下是我的代码:
HTML代码:
组件代码:
模型类:
我在控制台中看到这个错误:
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