角度图表不会更新数据更改

时间:2015-06-05 13:44:54

标签: angularjs angular-chart

我有一个Angular Chart栏,它应该在ng-click事件上更新,该事件从servlet中检索JSON数据。问题是即使数据在$ 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) {
        });

    };

1 个答案:

答案 0 :(得分:3)

看起来它是命名问题或控制器绑定到模板的方式

尝试将ng-controller="mygraphcontroller chart"更改为ng-controller="mygraphcontroller"

您还将它绑定到两个不同的div,尝试将其绑定到覆盖整个文件的div,顶级div设置为ng-controller="mygraphcontroller"