如何使用* ngFor迭代对象键?

时间:2016-05-05 08:42:57

标签: angular typescript object ngfor

我一直在挖掘,发现我可以使用以下内容在对象上使用* ngFor:

 <div *ngFor="#obj of objs | ObjNgFor">...</div>

其中ObjNgFor管道是:

@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]);
    }
}

但是,当我有这样的对象时:

{
"propertyA":{
    "description":"this is the propertyA",
    "default":"sth"
 },
"propertyB":{
    "description":"this is the propertyB",
    "default":"sth"
 }
}

我不太确定如何提取物业A&#39;和&#39; propertyB&#39;,以便可以从* ngFor指令访问它。有什么想法吗?

更新

我想要做的是呈现以下HTML:

        <div *ngFor="#obj of objs | ObjNgFor" class="parameters-container">
            <div class="parameter-desc">
                {{SOMETHING}}:{{obj.description}}
            </div>
        </div>

某些内容等于propertyApropertyB(这就是对象的结构)。所以,这将导致:

propertyA:this is the propertyA
propertyB:this is the propertyB

5 个答案:

答案 0 :(得分:15)

或者不是创建管道并将对象传递给* ngFor,而是将Object.keys(MyObject)传递给* ngFor。它返回与管道相同,但没有麻烦。

在TypeScript文件中:

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

在模板(html)上:

*ngFor="let item of list"

答案 1 :(得分:12)

只需从管道中返回键而不是值,然后使用键来访问值:

let代替{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

另见Select based on enum in Angular2

答案 2 :(得分:10)

更新

6.1.0-beta.1 KeyValuePipe中引入https://github.com/angular/angular/pull/24319

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

<强> Plunker Example

以前的版本

你可以试试这样的事情

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

然后在你的模板上

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

Plunker

答案 3 :(得分:0)

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>

答案 4 :(得分:0)

此示例不使用管道

*ngFor="let Value bof Values; let i = index"

{{i}}