简体   繁体   中英

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:

<% 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.

Instead you can declare an array in *.component.ts with your texts like this

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

    <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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