0

Iv'e got a few angular vars :

$scope.clicks_17        =   0;
$scope.clicks_18        =   0;
$scope.clicks_19        =   0;

Then i want to access them like that, with dynamic variables :

$scope.more =   function(){
                    date = new Date;
                    var h = date.getHours();
                    $scope.clicks_'+h' += 1;
                 }

But i can't make it work .. Any idea ? It means that the variable "h" needs to be "glued" at the end of the variable "clicks_".

Thank you !

1 Answers1

2

Any time you want to access a JavaScript object's property through a string, you can do so using array notation. You can do something like this:

$scope.more = function(){
  date = new Date;
  
  var h = date.getHours();
  $scope['clicks_' + h] += 1;
}
Fernando Lujan
  • 650
  • 7
  • 9