如何从db获取数据

时间:2016-07-27 07:07:50

标签: javascript angularjs typescript angular

我有一个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(); 
    });
      }
         }

My Observable,

   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);
  }

任何人都可以建议帮助......

3 个答案:

答案 0 :(得分:1)

&#13;
&#13;
ngOnInit(){
  this._service.getList().subscribe(details => { 
    this.details = details; 
    for(let detail of this.details) {
       console.log(detail.firstname);
    }
  });
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你需要用大括号包装你的console.log,如下所示:

public function search($params,$catId)
{
    $query = YourModel::find();

    // filter query object with the passed value.
    $query->andWhere(['your_search_column' => $catId]);



    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    $query->andFilterWhere([
        // your filtering codes
    ]);

    return $dataProvider;
}

这确保您不会将其记录到控制台,除非您已获取数据。

答案 2 :(得分:0)

按照以下建议使用它。

ngOnInit(){

  this._service.getList().subscribe((data) =>{    
    this.details = data; 

    this.details.forEach((detail)=>{
          console.log(detail.firstname);  
    })

  });

}