Iterate over object in Angular

前端 未结 17 1630
攒了一身酷
攒了一身酷 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:38

    I had a similar issue, built something for objects and Maps.

    import { Pipe } from 'angular2/core.js';
    
    /**
     * Map to Iteratble Pipe
     * 
     * It accepts Objects and [Maps](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
     * 
     * Example:
     * 
     *  
    * key {{keyValuePair.key}} and value {{keyValuePair.value}} *
    * */ @Pipe({ name: 'mapToIterable' }) export class MapToIterable { transform(value) { let result = []; if(value.entries) { for (var [key, value] of value.entries()) { result.push({ key, value }); } } else { for(let key in value) { result.push({ key, value: value[key] }); } } return result; } }

提交回复
热议问题