简体   繁体   English

如何在角度模板中增加变量值

[英]how to increment variable value in angular template

I'm new to angular and want to increment value while printing as done in JAVA as follows:我是 angular 新手,想在 JAVA 中打印时增加值,如下所示:

<% int count=1; %>
<%=count++%> This is line with Text
<%=count++%> This is another line with different text
<%=count++%> This is line with different text
<%=count++%> This is last line

Maybe this could be done only in html with some weird ngIf hacks, but that's not probably the way to go.也许这只能在带有一些奇怪的 ngIf hacks 的 html 中完成,但这可能不是要走的路。

Instead you can declare an array in *.component.ts with your texts like this相反,您可以使用这样的文本在*.component.ts中声明一个数组

lines = [
        'This is line with Text',
        'This is another line with different text',
        'This is line with different text',
        'This is last line',
    ];

and in *.component.html do an *ngFor并在*.component.html .component.html 中执行 *ngFor

    <div *ngFor="let line of lines; let index = index">{{ index + 1 }}: {{line}}</div>

It will output:它将输出:

<div>1: This is line with Text</div>
<div>2: This is another line with different text</div>
<div>3: This is line with different text</div>
<div>4: This is last line</div>

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

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