How to iterate object keys using *ngFor?

后端 未结 5 581
温柔的废话
温柔的废话 2020-11-27 20:46

I\'ve been digging around, and found out that I can use the following to use *ngFor over an object:

 
...
相关标签:
5条回答
  • 2020-11-27 20:58

    Just return the keys from the pipe instead of the values and then use the keys to access the values:

    (let instead of # in the beta.17)

    @Pipe({ name: 'ObjNgFor',  pure: false })
    export class ObjNgFor implements PipeTransform {
        transform(value: any, args: any[] = null): any {
            return Object.keys(value)//.map(key => value[key]);
        }
    }
    
    @Component({
        selector: 'my-app',
        pipes: [ObjNgFor],
        template: `
        <h1>Hello</h1>
     <div *ngFor="let key of objs | ObjNgFor">{{key}}:{{objs[key].description}}</div>    `,
    })
    export class AppComponent {
      objs = {
        "propertyA":{
          "description":"this is the propertyA",
          "default":"sth"
        },
        "propertyB":{
          "description":"this is the propertyB",
          "default":"sth"
        }
      };
    }
    

    Plunker example

    See also Select based on enum in Angular2

    0 讨论(0)
  • 2020-11-27 21:05

    Or instead of creating a pipe and passing an object to *ngFor, just pass Object.keys(MyObject) to *ngFor. It returns the same as the pipe, but without the hassle.

    On TypeScript file:

    let list = Object.keys(MyObject); // good old javascript on the rescue
    

    On template (html):

    *ngFor="let item of list"
    
    0 讨论(0)
  • 2020-11-27 21:05

    keys.pipe.ts

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({ name: 'keys' })
    export class KeysPipe implements PipeTransform {
        transform(obj: Object, args: any[] = null): any {
            let array = [];
            Object.keys(obj).forEach(key => {
                array.push({
                    value: obj[key],
                    key: key
                });
            });
            return array;
        }
    }
    

    app.module.ts

    import { KeysPipe } from './keys.pipe';
    
    @NgModule({
      declarations: [
        ...
        KeysPipe
      ]
    })
    

    example.component.html

    <elem *ngFor="let item of obj | keys" id="{{ item.key }}">
        {{ item.value }}
    </elem>
    
    0 讨论(0)
  • 2020-11-27 21:07

    no use pipes this it example

    *ngFor="let Value bof Values; let i = index"
    
    {{i}}
    
    0 讨论(0)
  • 2020-11-27 21:13

    Update

    In 6.1.0-beta.1 KeyValuePipe was introduced https://github.com/angular/angular/pull/24319

    <div *ngFor="let item of {'b': 1, 'a': 1} | keyvalue">
      {{ item.key }} - {{ item.value }}
    </div>
    

    Plunker Example

    Previous version

    You could try something like this

    export class ObjNgFor implements PipeTransform {
        transform(value: any, args: any[] = null): any {
            return Object.keys(value).map(key => Object.assign({ key }, value[key]));
        }
    }
    

    And then on your template

      <div *ngFor="let obj of objs | ObjNgFor">
       {{obj.key}} - {{obj.description}}
      </div>
    

    Plunker

    0 讨论(0)
提交回复
热议问题