1

nvd3 angular error: [$rootscope:infdig] 10 $digest() iterations reached. aborting!

I'm getting this error using nvd3 angular directive. Afaik this error happens when you modify models from within the view. But I don't think I'm doing that.

Here is the code:

//controller

var options = getChartOptions();
var data = getChartData();

$scope.chart = {
    data: data,
    options: options
};

//view

<nvd3 class="metrics-chart" options="chart.options" data="chart.data"></nvd3>

Here is the full controller:

var options = getChartOptions();
var data = getChartData();

$scope.chart = {
    data: data,
    options: options
};

function getChartOptions(){
    return ChartConfigService.getChartOptions();
}


function getChartData(){
    var data = [];
    var colors = {
        'GET': '#f00',
        'POST': '#0f0',
        'PUT': '#00f',
        'DELETE': '#ff0'
    };

    var verbs = ['GET', 'POST', 'PUT', 'DELETE'];

    verbs.forEach(function(verb){
        var i = 0;
        var values = [];
        var now = moment();

        while ( i < 10 ) {
            values.push({
                x: moment(now).add(i, 'minutes'),
                y: Math.round(Math.random() * 10)
            });

            i++;
        }

        data.push({
            values: values,      //values - represents the array of {x,y} data points
            key: verb, //key  - the name of the series.
            color: colors[verb]  //color - optional: choose your own line color.
        });
    });

    return data;
}
chovy
  • 72,281
  • 52
  • 227
  • 295

2 Answers2

1

This error occurs due to the fact that some unspecified property in options incorrectly assigned by default, and is constantly changing. In my case it was the height. When I specified it in options, the error has disappeared

wanderses
  • 11
  • 1
1

Your Service looks like it could create a new Object here:

ChartConfigService.getChartOptions();

If so...

Problem: Afaik there will be a new watcher on that new Object leading to another $digest-cycle. I guess the Directive 'nvd3' is using a two-way-data-binding on 'options' and is trying to resolve it again leading to another recursive call to:

ChartConfigService.getChartOptions();

Solution: Don't return a new Object inside:

ChartConfigService.getChartOptions();

-- OR --

// var options = getChartOptions();
// function getChartOptions(){ ... }
var options = ChartConfigService.getChartOptions();

-- OR --

var optionsData = ChartConfigService.getChartOptions();
function getChartOptions(){
    return optionsData;
}
David Renner
  • 454
  • 3
  • 15