[英]How to get the data from db
我有一个API,我想通过它在某些条件下显示数据,在这里我得到的详细信息如下:
我的服务组件,
@Injectable()
export class GetAllList {
id = localStorage.getItem('social_id')
private _productUrl = 'http://localhost/a2server/index.php/profile/getProfile/'+this.id;
constructor(private _http: Http) { }
getList(): Observable<IDetails[]> {
return this._http.get(this._productUrl)
.map((response: Response) => {
return <IDetails[]> response.json();
});
}
}
我的观察,
export interface IDetails{
profile_id:number;
firstname: string;
lastname: string;
profilename: string;
phone:string;
email:string;
address:string;
}
我正在主要组件中使用这些服务
ngOnInit(){
this._service.getList()
.subscribe(details => this.details = details);
}
这很好用,如果我想在控制台中检查名字,我该怎么办?是这样吗.....
ngOnInit(){
this._service.getList()
.subscribe(details => this.details = details);
console.log(this.details[0].firstname);
}
任何人都可以建议帮助吗……
ngOnInit(){ this._service.getList().subscribe(details => { this.details = details; for(let detail of this.details) { console.log(detail.firstname); } }); }
您需要使用大括号将console.log包裹起来,如下所示:
ngOnInit(){
this._service.getList()
.subscribe(details => { this.details = details;
console.log(this.details[0].firstname);
}); }
这可以确保除非已获取数据,否则不要将其记录到控制台。
按照以下建议使用它。
ngOnInit(){
this._service.getList().subscribe((data) =>{
this.details = data;
this.details.forEach((detail)=>{
console.log(detail.firstname);
})
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.