Reputation: 1
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
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
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