简体   繁体   English

如何在模板 Angular 中增加变量?

[英]How to increment variable in template Angular?

I have the following template:我有以下模板:

<ng-container *ngFor="let documentsGroup of documents | groupBy: 'doctype'"><
   <tr *ngFor="let doc of documentsGroup.value; let i = index">{{num}</tr>
</ng-container>

I know that it is not good to call {{num}} in template, because it will be rendered each time.我知道在模板中调用{{num}}不好,因为每次都会渲染。

Component is:组件是:

public t = 1;
public get num() {
    return this.t++;
}

So, how to create variable num in template and increment it like {{num++}} ?那么,如何在模板中创建变量num并像{{num++}}一样递增它?

I have created a simple example.我创建了一个简单的示例。 But I recommend you use it in the component which change detection strategy changed to the onPush .但我建议您在更改检测策略更改为onPush的组件中使用它。

Here is the link to the stackblitz example.这是 stackblitz 示例的链接。 https://stackblitz.com/edit/ngfor-example-rad9zs?file=app%2Fapp.component.html https://stackblitz.com/edit/ngfor-example-rad9zs?file=app%2Fapp.component.html

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

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