Angular2迭代对象数组

时间:2017-04-06 12:26:09

标签: angular

我有一个如下数组:

causes: any= [
{
    'Specification': {
        'Missing':false,
        'Unclear':false,
        'Wrong':false,
        'Changed':false,
        'Better Way':false,
    },
    'Design': {
        'Missing':false,
        'Unclear':false,
        'Wrong':false,
        'Changed':false,
        'Better Way':false,
    },
    'Code': {
        'Missing':false,
        'Unclear':false,
        'Wrong':false,
        'Changed':false,
        'Better Way':false,
    },
    'Documentation': {
        'Missing':false,
        'Unclear':false,
        'Wrong':false,
        'Changed':false,
        'Better Way':false,
    },
}]

如何在模板中迭代它?我试过像:

<div class="mdl-grid">
    <div *ngFor=" let chunk of causes | chunks: 2; let j = index; " class="mdl-cell mdl-cell--4-col">
        <label *ngFor=" let cause of chunk| values " #checkbox class="mdl-checkbox mdl-js-checkbox">
            <input type="checkbox" name="causes" class="mdl-checkbox__input">
            <span class="mdl-checkbox__label">{{cause}}</span>
        </label>
    </div>
</div>

但它仍然给了我[object Object]。我试图填充每个原因并反对它5个复选框。

2 个答案:

答案 0 :(得分:4)

在您提供的代码中,数组中只有一个对象。

我假设您的数组应该如下所示:

causes: any = [
    {
        name: 'Specification',
        values: {
            'Missing': false,
            'Unclear': false,
            'Wrong': false,
            'Changed': false,
            'Better Way': false,
        },
    },
    {
        name: 'Design',
        values: {
            'Missing': false,
            'Unclear': false,
            'Wrong': false,
            'Changed': false,
            'Better Way': false,
        },
    },
    {
        name: 'Code',
        values: {
            'Missing': false,
            'Unclear': false,
            'Wrong': false,
            'Changed': false,
            'Better Way': false,
        },
    },
    {
        name: 'Documentation',
        values: {
            'Missing': false,
            'Unclear': false,
            'Wrong': false,
            'Changed': false,
            'Better Way': false,
        },
    }
]

你的HTML应该是这样的

    <div class="mdl-grid">
        <div *ngFor=" let chunk of causes" class="mdl-cell mdl-cell--4-col">
            <div>Checkboxes for {{chunk.name}}</div>
            <div *ngFor=" let cause of chunk.values | keys">
               <label  #checkbox class="mdl-checkbox mdl-js-checkbox">
                   <input type="checkbox" [name]=" chunk.name + '_' + cause" class="mdl-checkbox__input">
                    <span class="mdl-checkbox__label">{{cause}}</span>
               </label>
            </div>
        </div>
    </div>

其中keys pipe是一个自定义管道,它返回对象的键,即字符串数组。管道代码:

<强>管

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'keys',
    pure: false
})
export class KeysPipe implements PipeTransform {
    transform(value, args: string[]): any {
        return Object.keys(value);
    }
}

答案 1 :(得分:2)

无法开箱即可迭代对象。但是,您可以使用如下所示的自定义管道。

sysuse nlsw88

table married race collgrad, col

--------------------------------------------------------------------
          |                college graduate and race                
          | ---- not college grad ----    ------ college grad ------
  married | white  black  other  Total    white  black  other  Total
----------+---------------------------------------------------------
   single |   355    256      5    616      132     53      3    188
  married |   862    224     12  1,098      288     50      6    344
--------------------------------------------------------------------

并将其用作

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

看看here