Chartjs-Prevent the wiping of an Array after routing Angular.js

0👍

The data being used in the table is deleted when the user navigates away from the chart controller page. When the user returns, this data is started fresh, hence the empty table on return.

To fix this, on page leave the data table should be saved in a service from $scope.data. On return, the data should be pulled from the service and back into $scope.data.

You can try something similar

// Untested code

app.factory("chartService", function() {
    var chartData;

    function saveData(data) {
      chartData = data;          
    }

    function getData() {
      return chartData;
    }

    return {
      saveData: saveData,
      getData: getData
    };
});

app.controller("chartCtrl", function($rootScope, $scope, $interval, chartService) {
    var data = chartService.getData();       
    if (data) {
      // Navigating back
      $scope.data = data;
    } else {
       // First visit of the session
       $scope.data = [[],[],[]];
    }
    
     // interval code ...

    $scope.$on('$destroy', function() {
          chartService.saveData($scope.data);
          $interval.cancel(stopChart);
    });
});

Leave a comment