user3100184
user3100184

Reputation: 1

Loop on the value in Angular2

I am trying to loop in Angular2 on some value, using code something like this:

if (true) {
    for ( var i = 0; i < 4; i++ ){
        console.log("the value of i is : " + i); 
    }
} else {
    console.log("in else part") ; 
}

In the console, I can see the value for i is always 0, so I'm pretty sure this is not the correct way to loop in Angular 2.

I even tried using:

for ( let i = 0; i < 4; i++ ){
    console.log("value : " + i) ; 
}

But the result is the same.

Can anyone please show me how do we loop/iterate in Angular 2?

Upvotes: 0

Views: 53

Answers (2)

C.Petrescu
C.Petrescu

Reputation: 83

when you use the typescript, the main is the type declaration. So, I think you must declare the type of i. that is,

let i: number;
for (i = 0; i < 4; i++) {
  console.log("value: " + i);
}

That's all.

Upvotes: 0

SrAxi
SrAxi

Reputation: 20005

Declare the i variable outside the for block scope.

let i: number;
for (i = 0; i < 4; i++){
  console.log("value : " + i) ; 
}

Upvotes: 2

Related Questions