Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular-charts not working with requirejs

i am trying to use angular-chart but i couldn't figure how to add dependencies correctly. i am getting following error Uncaught TypeError: Cannot read property 'defaults' of undefined in the angular-chart.js

(function (factory) {
  'use strict';
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
      define(['angular', 'chart.js'], factory);
  } else if (typeof exports === 'object') {
    // Node/CommonJS
      module.exports = factory(require('angular'), require('chart.js'));
  } else {
    // Browser globals
    factory(angular, Chart);
  }
}(function (angular, Chart) {
  'use strict';

  Chart.defaults.global.responsive = true;
  ....

because both angular and chart are undefined.

my require config is

'use strict';
require.config({
    baseUrl: '/',
    paths: {
		'angular': '/scripts/angular',
		'angular-route': '/scripts/angular-route',
		'ui-bootstrap': '/scripts/ui-bootstrap-tpls-0.13.0.min',
		'angular-animate': '/scripts/angular-animate.min',

		'chart': '/scripts/chart',
		'angular-chart': '/scripts/angular-chart',
		
		'data-utils': '/common/data-utils',
		'string-utils': '/common/string-utils',

        'app': '/config/app',
        'routes': '/config/routes'
    },
	shim: {
		'app': {
		    deps: ['angular', 'angular-route', 'ui-bootstrap', 'data-utils', 'string-utils', 'angular-chart']
		},
		
		'angular-route': {
			deps: ['angular']
		},
		'ui-bootstrap': {
		    deps: ['angular', 'angular-animate']
		},
		'angular-animate': {
		    deps: ['angular']
		},
		'angular-chart': {
		    deps: ['angular', 'chart']
		}
	}
});

require
(
    ['app'],
    function(app)
    {
        angular.bootstrap(document, ['ngAnimate', 'ui.bootstrap', 'app']);
    }
);

my controller is

define(['app'], function(app)
{
	app.controller('homeController',
    [
        '$scope',
        function($scope)
        {
            $scope.page =
            {
                title: 'Welcome to Easy Stitch'
            };

            $scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
            $scope.series = ['Series A', 'Series B'];

            $scope.data = [
              [65, 59, 80, 81, 56, 55, 40],
              [28, 48, 40, 19, 86, 27, 90]
            ];
        }
    ]);
});

every other dependency is working fine except for this one. please if anyone can help.

like image 463
mzain Avatar asked Sep 27 '22 20:09

mzain


2 Answers

I figure that I have to correct the dependency in my require config file and after that it is working fine.

require
(
    ['app'],
    function(app)
    {
        angular.bootstrap(document, ['ngAnimate', 'ui.bootstrap', 'chart.js', 'app']);
    }
);
like image 186
mzain Avatar answered Oct 20 '22 12:10

mzain


you can modify the angular-chart.js:

chart.js => chart,

(function (factory) {
  'use strict';
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module
    define(['angular', 'chart'], factory);
  } else if (typeof exports === 'object') {
    // Node/CommonJS
    module.exports = factory(require('angular'), require('chart'));
  } else {
    // Browser globals
    factory(angular, Chart);
  }
like image 43
wangd933 Avatar answered Oct 20 '22 11:10

wangd933