I have a div that will need to host a chart or table depending on an XHR response. In the chart case, I need the div contents to be replaced by a canvas element that chart.js uses to display a graph.
If I add the canvas element to the HTML code, angular-chart.js renders a graph. However, If I inject the canvas into a div using javascript, the canvas element will be in the dom but the chart will not be displayed.
How do I work around this?
HTML
<div ng-controller="ChartCtrl">
<div>
{{chart.name}}
This works (doughnut):
<canvas id="chart-{{$index}}" class="chart chart-doughnut" chart-data="chart.data" chart-labels="chart.labels"></canvas>
</div>
This Doesn't work ({{chart.type}}):
<div id="chartDiv"> </div>
</div>
Javascript
var myApp = angular.module('myApp', ['chart.js']);
// Create the controller, the 'ToddlerCtrl' parameter
// must match an ng-controller directive
myApp.controller('ChartCtrl', function ($scope) {
var chart_div = $('#chartDiv');
chart_div.empty();
canvas_html = '<canvas id="chart-2" class="chart chart-doughnut" chart-data="chart.data" chart-labels="chart.labels"></canvas>';
console.log(canvas_html)
chart_div.append(canvas_html);
//console.log(chart_div)
$scope.chart = {
name: 'Chart 1',
type: 'Doughnut',
labels: ['EVSC', 'ISB'],
data: [13, 44]
};
});
Plunker Example: http://plnkr.co/edit/9JkANojIl8EXRj3hJNVH?p=preview