Iterate over object in Angular

前端 未结 17 1335
攒了一身酷
攒了一身酷 2020-11-22 12:19

I am trying to do some things in Angular 2 Alpha 28, and am having an issue with dictionaries and NgFor.

I have an interface in TypeScript looking like this:

相关标签:
17条回答
  • 2020-11-22 12:53

    Here's a variation on some of the above answers that supports multiple transforms (keyval, key, value):

    import { Pipe, PipeTransform } from '@angular/core';
    
    type Args = 'keyval'|'key'|'value';
    
    @Pipe({
      name: 'mapToIterable',
      pure: false
    })
    export class MapToIterablePipe implements PipeTransform {
      transform(obj: {}, arg: Args = 'keyval') {
        return arg === 'keyval' ?
            Object.keys(obj).map(key => ({key: key, value: obj[key]})) :
          arg === 'key' ?
            Object.keys(obj) :
          arg === 'value' ?
            Object.keys(obj).map(key => obj[key]) :
          null;
      }
    }
    

    Usage

    map = {
        'a': 'aee',
        'b': 'bee',
        'c': 'see'
    }
    
    <div *ngFor="let o of map | mapToIterable">{{o.key}}: {{o.value}}</div>
      <div>a: aee</div>
      <div>b: bee</div>
      <div>c: see</div>
    
    <div *ngFor="let o of map | mapToIterable:'keyval'">{{o.key}}: {{o.value}}</div>
      <div>a: aee</div>
      <div>b: bee</div>
      <div>c: see</div>
    
    <div *ngFor="let k of map | mapToIterable:'key'">{{k}}</div>
      <div>a</div>
      <div>b</div>
      <div>c</div>
    
    <div *ngFor="let v of map | mapToIterable:'value'">{{v}}</div>
      <div>aee</div>
      <div>bee</div>
      <div>see</div>
    
    0 讨论(0)
  • 2020-11-22 12:53

    If you have es6-shim or your tsconfig.json target es6, you could use ES6 Map to make it.

    var myDict = new Map();
    myDict.set('key1','value1');
    myDict.set('key2','value2');
    
    <div *ngFor="let keyVal of myDict.entries()">
        key:{{keyVal[0]}}, val:{{keyVal[1]}}
    </div>
    
    0 讨论(0)
  • 2020-11-22 12:55

    The dictionary is an object, not an array. I believe ng-repeat requires an array in Angular 2.

    The simplest solution would be to create a pipe/filter that converts the object to an array on the fly. That said, you probably want to use an array as @basarat says.

    0 讨论(0)
  • 2020-11-22 12:58

    It appears they do not want to support the syntax from ng1.

    According to Miško Hevery (reference):

    Maps have no orders in keys and hence they iteration is unpredictable. This was supported in ng1, but we think it was a mistake and will not be supported in NG2

    The plan is to have a mapToIterable pipe

    <div *ngFor"var item of map | mapToIterable">

    So in order to iterate over your object you will need to use a "pipe". Currently there is no pipe implemented that does that.

    As a workaround, here is a small example that iterates over the keys:

    Component:

    import {Component} from 'angular2/core';
    
    @Component({
      selector: 'component',
      templateUrl: `
           <ul>
           <li *ngFor="#key of keys();">{{key}}:{{myDict[key]}}</li>
           </ul>
      `
    })
    export class Home {
      myDict : Dictionary;
      constructor() {
        this.myDict = {'key1':'value1','key2':'value2'};
      }
    
      keys() : Array<string> {
        return Object.keys(this.myDict);
      }
    }
    
    interface Dictionary {
        [ index: string ]: string
    }
    
    0 讨论(0)
  • 2020-11-22 12:59
    //Get solution for ng-repeat    
    //Add variable and assign with Object.key
    
        export class TestComponent implements OnInit{
          objectKeys = Object.keys;
          obj: object = {
            "test": "value"
            "test1": "value1"
            }
        }
        //HTML
          <div *ngFor="let key of objectKeys(obj)">
            <div>
              <div class="content">{{key}}</div>
              <div class="content">{{obj[key]}}</div>
            </div>
    
    0 讨论(0)
提交回复
热议问题