How to dependency injection using ocLazyLoad

六眼飞鱼酱① 提交于 2019-12-11 02:42:24

问题


I'm using ocLazyLoad and I have some external angular libraries (Like Chart.js and pascalprecht.translate) and I need to lazy load them in some routes, as you know, for the common angular module dependency injection should be like:

var angularApp = angular.module('myApp',
     ['oc.lazyLoad', 'pascalprecht.translate', 'chart.js']);

Now, I just need to lazy loading pascalprecht.translate in one of my controllers and also lazy loading chart.js, in another controller, but I still need to add inject them to myApp module but I don't know how to inject and I do not use $stateProvider

I tried this my controller that I needed chart.js:

//Load here.
//$ocLazyLoad.load('./panel/dist/test.js');
angular.module('myApp', ['chart.js', [
 './panel/dist/static/chart.min.js',
 './panel/dist/static/angular-chart.min.js'
]]);

$ocLazyLoad.load('./panel/dist/static/chart.min.js');
$ocLazyLoad.load('./panel/dist/static/angular-chart.min.js');

But I got this error:

angular-chart.min.js:10Uncaught Error: Chart.js library needs to included, see http://jtblin.github.io/angular-chart.js/


回答1:


First, you do not need to inject chart.js in your dependency injection, this should be your module:

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

Now, you want to have access to some libraries from different controllers (let's say routes), as you said you do not use $stateProvider which means you do not use ui-router (which is a third-party library to work with routes and URLs).

This is my suggestion (just a simple solution):

angularApp.config(function ($routeProvider, $locationProvider) {
    $routeProvider.when('/home', {
        templateUrl: 'views/home.html',
        controller: 'HomeController',
        resolve: {
            store: function ($ocLazyLoad) {
                return $ocLazyLoad.load(
                    {
                        serie: true,
                        name: "chart.js",
                        files: [
                            "./static/chart.min.js",
                            "./static/chart-angular.min.js",
                        ]
                    }
                );
            }
        }
    });

        $routeProvider.when('/needs-translate', { 
        templateUrl: 'views/needs-translate.html',
        controller: 'translateController',
        resolve: {
            store: function ($ocLazyLoad) {
                return $ocLazyLoad.load(
                    {
                        serie: true,
                        name: "pascalprecht.translate",
                        files: [
                            "./static/translate.js"
                        ]
                    }
                );
            }
        }
    });

    $routeProvider.otherwise({
        redirectTo: '/home'
    });

    // use the HTML5 History API
    $locationProvider.hashPrefix = '!';
    $locationProvider.html5Mode(true);
});

By the way, if you are using ui.router, this Github issue would be useful for you



来源:https://stackoverflow.com/questions/39875533/how-to-dependency-injection-using-oclazyload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!