score:6

Accepted answer

there appears to be an issue with the charts library modifying the existing object on the root scope, and thereby ignoring it forever afterward. i can't really trace down what is doing it, but here's a fix for you: http://plnkr.co/edit/jdqfv62fsexaqj6o7je8

here is what you had

  scope.$watch('config', function(newval) {
    if(angular.isdefined(newval)) {
      if(charts) {
        charts.destroy();
      }
      var ctx = element[0].getcontext("2d");
      charts = new chart(ctx, scope.config);
      //scope.$emit('create', charts);
    }
  });

above, you can see that you're passing scope.config directly into the charts method. that appears to be modifying the data somehow, and since that's passed by reference, you're actually modifying $rootscope.sales.charts. if you copy that object and use it locally like below, you don't have that problem.

here's how i fixed it.

  scope.$watch('config', function(newval) {
    var config = angular.copy(scope.config);
    if(angular.isdefined(newval)) {
      if(charts) {
        charts.destroy();
      }
      var ctx = element[0].getcontext("2d");
      charts = new chart(ctx, config);
      //scope.$emit('create', charts);
    }
  });

you can see that instead of passing that object directly in, we use angular to make a copy (angular.copy()), and that's the object we pass in.

score:2

i think it has relation with the id of the canvas where you are drawing. i've had this problem too amd it was because i was using the same id for the canvas of two graphs in different views. be sure that those ids are different and that the javasrcipt of each graph is in the controller of each view or in each view itself.

taking a look at your pluker i see that you are using the same html for the graph and i guess that when angular moves from one of your views to the other thinks that the graph is already drawn. differentiating two graphs will solve the problem. i don't know of there is any other approach that allows using the same html for the canvas of the graph.

hope it helps you solve it


Related Query

More Query from same tag