1

I am trying to update a $scope variable:

ex:

$scope.variable_1
$scope.variable_2
...

I would like to update it this way:

for (i=0; i<2; i++) {    
  $scope.variable_$i = 1;
}

What I need is to get access to the "$scope.variable_1" using the index "i" in each iteration.

any suggestion? Thanks in advance.

Albert Prats
  • 786
  • 4
  • 15
  • 32

1 Answers1

2

In javascript you can access variables by their name :

for (i=0; i<2; i++) {    
  $scope['variable_'+i] = 1;
}

See : Dynamically access object property using variable

Also, accessing properties this way should be avoided if you can use an array instead.

Community
  • 1
  • 1
mathieu
  • 30,974
  • 4
  • 64
  • 90