Dygraphs not working with ng-repeat

I'm new to AngularJS and building a dashboard with dygraphs.

Tried to put the example code from the dygraphs website in an ng-repeat-list, just to test. Expected the same sample graph for every x in y. Unfortunately the graph doesn't get drawn, just the axes, console doesn't show any errors.

<li ng-repeat="x in y">
    <div id="graph">
            new Dygraph(document.getElementById("graph"),
                   [ [1,10,100], [2,20,80], [3,50,60], [4,70,80] ],
                   { labels: [ "x", "A", "B" ] });

If I remove ng-repeat, it works though (single graph) – so the dygraphs-code is valid. Of course it doesn't make sense to draw the graphs directly in the view like I did here, still I wonder why it doesn't work. Am I missing some general point here?


Your problem is that Angular will repeat your <div id="graph"> n times. So you now have n times div with id of 'graph' which are siblings. Therefore, when you call document.getElementById('graph'), that won't work very well.

That said, I don't know how well script tags inside ng-repeat works either, seems like a very strange use case.

The proper way to do this (as with all DOM related operations), is to use a directive. Here's an example:


var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {
    $scope.graphs = [
            data: [ [1,10,100], [2,20,80], [3,50,60], [4,70,80] ],
            opts: { labels: [ "x", "A", "B" ] }

            data: [ [1,10,200], [2,20,42], [3,50,10], [4,70,30] ],
            opts: { labels: [ "label1", "C", "D" ] }


myApp.directive('graph', function() {
    return {
        restrict: 'E', // Use as element
        scope: { // Isolate scope
            data: '=', // Two-way bind data to local scope
            opts: '=?' // '?' means optional
        template: "<div></div>", // We need a div to attach graph to
        link: function(scope, elem, attrs) {

            var graph = new Dygraph(elem.children()[0], scope.data, scope.opts );


<div ng-controller="MyCtrl">
    <graph ng-repeat="graph in graphs" data="graph.data" opts="graph.opts"></graph>


Hope this helps!

Need Your Help

How can you format a persisted column as a padded integer string [SQL Server 2008]

sql sql-server-2008 formula persisted-column

What formula would I use in a persisted column so that I can add the two columns below together, the first must be padded to 5 characters:

How to validate form using several functions in javascript

javascript validation field

The two function work perfectly but im only able to use one at a time, the form should check password and link the user to other pages based on the selected radio button. Is there a way to make it ...