print attributes values from JSON array in Angular2

后端 未结 2 1811
我在风中等你
我在风中等你 2021-01-27 23:51

I\'m using Angular2 and I have retrieved some data from Firebase in this way:

dataset: any;
onGetData() {
    this._dataService.getAllData()
        .subscribe(
         


        
相关标签:
2条回答
  • 2021-01-28 00:30

    You can only iterate over an array using ngFor. In your case you need to implement a custom pipe to iterate over keys of an object.

    Something like that:

    @Pipe({name: 'keyValues'})
    export class KeysPipe implements PipeTransform {
      transform(value, args:string[]) : any {
        let keys = [];
        for (let key in value) {
          keys.push({key: key, value: value[key]);
        }
        return keys;
      }
    }
    

    and use it like that:

    <span *ngFor="#entry of dataset | keyValues">           
      Title: {{entry.value.title}}
    </span>
    

    See this question for more details:

    • How to display json object using *ngFor
    0 讨论(0)
  • 2021-01-28 00:40

    In your view you need

    <div *ngFor='#data of dataset'>
        {{ data.title }} -
    </div>
    
    0 讨论(0)
提交回复
热议问题