简体   繁体   English

如何使用requirejs将chart.js与angular-chart一起使用

[英]how to use chart.js with angular-chart using requirejs

Trying to implement lazy load using requirejs . 尝试使用requirejs实现延迟加载。 Everything is fine when I am not using charts. 当我不使用图表时,一切都很好。 But when I want to use charts(angular charts), not going to sucseed! 但是,当我想使用图表(角度图表)时,不会成功! Using chart.js with angular-chart . chart.jsangular-chart一起使用

Here is main.js: 这是main.js:

 require.config({ 
baseUrl: "http://localhost/ums/angular/js",
    paths: {
        'angular': 'lib/angular.min',
        'ngRoute': 'lib/angular-route.min',
        'flash': 'lib/angular-flash',
        'angular-loading-bar': 'lib/loading-bar.min',
        'ngAnimate': 'lib/angular-animate.min',
        'ui.bootstrap': 'lib/ui-bootstrap-tpls-0.12.0',
        'uniqueField': 'admin/directives/angular-unique',
        'input_match': 'admin/directives/angular-input-match',
        'uniqueEdit': 'admin/directives/angular-unique-edit',
        'angularAMD': 'lib/angularAMD.min',
        'chart.js': 'lib/Chart.min', 
        'angular-chart':'lib/angular-chart.min',   
        'app': 'admin/app',
        },
        waitSeconds: 0,
         shim: { 
         'angular': { exports: 'angular'},
        'angularAMD': { deps: ['angular']},
        'angular-chart': { deps: ['angular','chart.js']},
        'ngRoute':{ deps: ['angular']},
        'angular-loading-bar':{ deps:['angular'] },
        'ngAnimate': { deps:['angular'] } ,
        'ui.bootstrap': {deps: ['angular'] },
        'uniqueField': {deps: ['angular'] },
        'input_match': {deps: ['angular'] },
        'uniqueEdit': {deps: ['angular'] },
        'flash': {deps: ['angular'] },
        },
        deps: ['app']
    });

Here is app.js: 这是app.js:

var base_url="http://localhost/ums/";
define(['angularAMD', 'ngRoute','flash','angular-loading-bar','ngAnimate','uniqueField','input_match','angular-chart'], function (angularAMD) {
var app = angular.module('angularapp', ['ngRoute','flash','angular-loading-bar','ngAnimate','uniqueField','input_match','angular-chart']);  
app.config(['$routeProvider', function($routeProvider){
    $routeProvider
        .when('/dashboard', angularAMD.route({
                title : 'Dashboard',
                controller : 'dashboardCtrl',
                templateUrl : base_url+'angular/partials/admin/dashboard.php',
                controllerUrl: base_url+'angular/js/admin/controllers/dashboardCtrl.js'
            }))

//.......................all routing ............//
 .otherwise({
            redirectTo : '/dashboard'
        });
}]);
app.run(['$rootScope', '$route', function($rootScope, $route) {
    $rootScope.$on('$routeChangeSuccess', function() {
        document.title = $route.current.title;
    });
}]);


  // Bootstrap Angular when DOM is ready
    return angularAMD.bootstrap(app);

});

How to implement dependancy between them? 如何实现它们之间的依赖关系? Any suggestions? 有什么建议么? any examples? 有什么例子吗?

From the documentation - http://requirejs.org/docs/api.html 从文档-http://requirejs.org/docs/api.html

If a module ID has one of the following characteristics, the ID will not be passed through the "baseUrl + paths" configuration, and just be treated like a regular URL that is relative to the document: 如果模块ID具有以下特征之一,则该ID将不会通过“ baseUrl +路径”配置传递,而只会被视为相对于文档的常规URL:
• Ends in ".js". •以“ .js”结尾。
• Starts with a "/". •以“ /”开头。
• Contains an URL protocol, like "http:" or "https:". •包含URL协议,例如“ http:”或“ https:”。

This will kick in for your chart.js module and RequireJS will attempt to load chart.js from the directory that contains the HTML page running RequireJS 这将启动您的chart.js模块,RequireJS将尝试从包含运行RequireJS的HTML页面的目录中加载chart.js

Note - you should be able to see this in your Developer Tools > Network tab. 注意-您应该可以在开发人员工具>网络标签中看到此内容。 Note that the request for chart.js doesn't go to the path that you expect. 请注意,对chart.js的请求不会转到您期望的路径。


This seems like a bug (with angular-chart) - see a similar issue for typeahead.js - https://github.com/twitter/typeahead.js/issues/1211 这似乎是一个错误(带有角度图)-看到typeahead.js的类似问题-https: //github.com/twitter/typeahead.js/issues/1211


There are a couple of ways to fix / workaround this 有几种方法可以解决/解决此问题

  1. Modify angular-chart code to make the expected module ID as something without a dot - say chartjs . 修改angular-chart代码以使期望的模块ID为不chartjs Then change chart.js in your above configuration to chartjs. 然后将上述配置中的chart.js更改为chartjs。 This would be correct way. 这是正确的方法。

  2. Rename you chart.min.js file to chart.js and put it in the same folder as your html file running RequireJS. 将您的chart.min.js文件重命名为chart.js,并将其与运行RequireJS的html文件放在同一文件夹中。 This would at best be a very very temporary fix. 充其量只是一个非常非常临时的解决方案。

  3. Set nodeIDCompat to true - this will make module ID something.js equivalent to something . 将nodeIDCompat设置为true-这将使模块ID something.js等同于something Then change the module ID in your configuration to chart . 然后将配置中的模块ID更改为chart So something like 所以像

     require.config({ nodeIdCompat: true, paths: { 'chart': base_url + '/lib/Chart.min', ... 

If you are using r.js to compress / consolidate, you might want to just test that too before settling on this workaround. 如果您正在使用r.js进行压缩/合并,则可能需要先进行测试,然后再解决此变通办法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM