简体   繁体   English

Angular2:html组件中的循环打字稿字典

[英]Angular2: loop typescript dictionary in html component

I'm developing an Angular 2 application and I want to loop a Typescript dictionary to show its data. 我正在开发一个Angular 2应用程序,我想循环一个Typescript字典来显示它的数据。

The interface I want to show is: 我想要显示的界面是:

export interface IProductionOrderDetail {
    productionOrderName: string;
    productCode: string;
    batches: IBatch[];
    levels: IAggLevel[];
    variableData: IVarData[];
    codeStatistics: ICodeStatistics;
}

export interface ICodeStatistics {
    [index: number]: {
        commissionedCodes: number;
        decommissionedCodes: number;
        aggregatedCodes: number;
    }
}

I know that I can do this in TypeScript to loop the dictionary: 我知道我可以在TypeScript中执行此操作来循环字典:

for (let key in myDictionary) {
    let value = myDictionary[key];
    // Use `key` and `value`
}

So, I have decided to use it in the same way in a html component: 所以,我决定在html组件中以相同的方式使用它:

<div class="row" *ngFor="let key in prodetail.codeStatistics">
    <div class="col-md-1">{{queryLevelName(key)}}</div>
    <div class="col-md-1">{{prodetail.codeStatistics[key].commissionedCodes}}</div>
    <div class="col-md-1">{{prodetail.codeStatistics[key].decommissionedCodes}}</div>
    <div class="col-md-1">{{prodetail.codeStatistics[key].aggregatedCodes}}</div>
</div>

But it seems that there is something wrong with the in in *ngFor="let key in prodetail.codeStatistics" because I get the following error: 但似乎有什么毛病in*ngFor="let key in prodetail.codeStatistics" ,因为我得到以下错误:

Can't bind to 'ngForIn' since it isn't a known property of 'div'. 无法绑定到'ngForIn',因为它不是'div'的已知属性。 (" ]*ngFor="let key in prodetail.codeStatistics"> (“] * ngFor =”让密钥进入prodetail.codeStatistics“>

How can I loop over the dictionary?a 我怎么能循环字典呢?

In latest version of Angular (6.0.0+) : So great to find the best solution from Angular itself provides pipe to loop through JSON , and its keyvalue 在角的最新版本(6.0.0+):通过JSON,其所以伟大的发现从自身角度最佳的解决方案提供管道循环keyvalue

<div *ngFor='let item of jsonObj | keyvalue'>
   Key: {{item.key}}

    <div *ngFor='let obj of item.value'>
        {{ obj.title }}
        {{ obj.desc }}
    </div>

</div>

WORKIGN DEMO WORKIGN DEMO


Previously : 以前:

As angular doesn't support jsonObject looping from template side and there is no ngForIn directive yet , 由于angular不支持从模板端循环jsonObject,并且还没有ngForIn指令,

You can loop through the data only if its array with *ngForOf , but if you want to loop through json or dictionary You can do it like 你可以只在数组中使用* ngForOf循环数据,但如果你想循环遍历json或字典你可以这样做

Component side : 组件方面:

objectKeys = Object.keys;

Template side : 模板方面:

<div class="row" *ngFor="let key of objectKeys(prodetail.codeStatistics)">
    <div class="col-md-1">{{queryLevelName(key)}}</div>
    <div class="col-md-1">{{prodetail.codeStatistics[key].commissionedCodes}}</div>
    <div class="col-md-1">{{prodetail.codeStatistics[key].decommissionedCodes}}</div>
    <div class="col-md-1">{{prodetail.codeStatistics[key].aggregatedCodes}}</div>
</div>

WORKING DEMO 工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM