John John - 6 months ago 20
Javascript Question

javascript/angularjs concatenate String with number for loop

var number = 0;
var date = "2016-05-10"
$scope.test = date;


I would like to make a loop with the variable number in order to get as result

$scope.test1 = 2016-05-10
$scope.test2 = 2016-05-10
$scope.test3 = 2016-05-10


variable
$scope.test
is not an array but a string

Answer

I think you're undervaluing or ignorant of the purposes of arrays and ng-repeat. From your comments, it sounds like you want to be able to iterate through multiple groups of data sets, each group producing a chart, and I'm going to use your earlier comment as a permit to solve what I think is your problem, rather than your literal question.

Take a look at this example I made, a simple modification of your own JSFiddle sample. All I'm doing is placing the objects that contain the data sets (I copied and tweaked the one you had) into an array, then iterating over them, passing the data to charts with ng-repeat (you may need to scroll down in the view pane to see the second chart).

In your JS controller:

// Chart.js Data
    $scope.chartDataArray = [{
      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
      datasets: [
        {
          label: 'My First dataset',
          // ... All the stuff for coloration
          data: [65, 59, 80, 81, 56, 55, 40]
        },
        {
          label: 'My Second dataset',
          // ... All the stuff for coloration
          data: [28, 48, 40, 19, 86, 27, 90]
        }
      ]
    },{
      labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
      datasets: [
        {
          label: 'My First dataset',
          // ... All the stuff for coloration
          data: [0, 1, 2, 3, 4, 5, 6]
        },
        {
          label: 'My Second dataset',
          // ... All the stuff for coloration
          data: [12, 10, 8, 6, 4, 2, 0]
        }
      ]
    }];

In your HTML view:

<canvas ng-repeat="data in chartDataArray" tc-chartjs-line chart-options="options" chart-data="data" auto-legend ng-click="chartClick($event)" chart="chart"></canvas>

This is basically just a foreach loop.