Akhil Srivastava
Akhil Srivastava

Reputation: 16

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

Upvotes: 0

Views: 806

Answers (1)

0xBobby
0xBobby

Reputation: 311

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>

Upvotes: 2

Related Questions