我有一个角图表栏,它应该在从 servlet 检索 JSON 数据的 ng-click 事件上更新。问题是即使 $scope 上的数据已更新,图表也不会反映更新。为什么会这样?有没有办法手动重绘图表?我在 Angular Chart 网站上找不到太多文档。
编辑:根据要求,我还附加了模板,这是否与我在两个不同的 div 上有相同的控制器这一事实有关?
<div class="row" ng-controller="mygraphcontroller as chart">
<!-- other things -->
<a ng-click="applyFilterGraph()" class="btn btn-lg btn-primary btn-raised">Apply</a>
<!-- other things -->
</div>
<div class="row">
<div class="col-md-12" ng-controller="mygraphcontroller as chart">
<div class="panel panel-default">
<div class="panel-heading">*****</div>
<div class="panel-body">
<canvas id="bar" class="chart chart-bar" data="data" labels="labels"></canvas>
</div>
</div>
</div>
</div>
app.controller('mygraphcontroller', ['$scope', '$http', '$log', function($scope,$http,$log) {
$scope.labels = [];
$scope.data = [];
// THE FOLLOWING POST WORKS FINE ON RUNTIME
$http({
method: 'POST',
url: '/myservlet',
params: { param1: 'param1' }
}).success(function(result) {
var slabels = [];
var array = [];
var sdata = [array];
for (var i = 0; i < result.length; i++) {
slabels.push(result[i].company.companyDescription);
array.push(result[i].value);
}
$scope.labels.push.apply($scope.labels,slabels);
$scope.data.push.apply($scope.data,sdata);
}).error(function(result) {
});
// THIS ONE DOES NOT WORK, CHART IS NOT UPDATED
$scope.applyFilterGraph = function() {
$scope.labels = [];
$scope.data = [];
$http({
method: 'POST',
url: '/myservlet',
params: { param2: 'param2' }
}).success(function(result) {
var slabels = [];
var array = [];
var sdata = [array];
for (var i = 0; i < result.length; i++) {
slabels.push(result[i].company.companyDescription);
array.push(result[i].value);
}
$scope.labels.push.apply($scope.labels,slabels);
$scope.data.push.apply($scope.data,sdata);
}).error(function(result) {
});
};