I'm currently working on building a chart with chart.js and I want to populate the chart with data from the backend. However, before that, I need to call a function that is located in my controller. My goal is to invoke this function directly from index.html.
INDEX:
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: $scope.getChartLabels(),
datasets: [{
label: '# of Votes',
data: $scope.getChartData(),
backgroundColor: [
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: true,
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
CONTROLLER:
(...)
.controller('DashboardController', function ($scope) {
$scope.getChartLabels = function() {
return ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"];
}
$scope.getChartData = function() {
return [12, 19, 3, 5, 2, 3];
}
}
(...)
Is there a way to properly call the Angular functions getChartLabels() or getChartData() from index.html without resorting to bad practices?