ocLazyLoad to load controllers from an Angularjs SPA

Asked

Viewed 679 times

1

Hello, community!

I wonder if it’s a good idea to use ocLazyLoad to carry the controllers an application (Single Page Application - SPA) of Angularjs.

I’m using UI Router in place of ngRoute.

The code I have works perfectly (in the example, it’s simplified, just for demonstration purposes).

'use strict';

var minhaAplicacao = angular.module('minhaAplicacao', ['oc.lazyLoad', 'ui.router']);

angular.module('minhaAplicacao').config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/home');
    $stateProvider
        .state('home', {
            url: '/home',
            templateUrl: 'partials/home/home.html',
            data: {pageTitle: 'Home'},
            resolve: {
                service: ['$ocLazyLoad', function ($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        serie: true,
                        files: [
                            'partials/home/home.controller.js'
                        ]
                    });
                }]
            }
        });
<!DOCTYPE html>
<html ng-app="minhaAplicacao">
    <head>
        <meta charset="utf-8">
        <title data-ng-bind="'GEOPS | ' + $state.current.data.pageTitle">GEOPS</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <link rel="stylesheet" href="bower/vendor/bootstrap-3.3.6/css/bootstrap.min.css">
    </head>
    <body>
        <div ui-view></div>
        <script src="path/to/angular.js"></script>
        <script src="path/to/ocLazyLoad.js"></script>
        <script src="path/to/ui-router.js"></script>
    </body>
</html>

My doubt is as to the implications of this practice.

It’s wrong to do the lazyloading of controllers Angularjs?

Is there a safety or scalability problem in this type of practice?

2 answers

2

Well that goes from your project, I think it valid to use it to carry the controllers, services etc, when for example your application has to fit the user profile. Example: the operator user profile sees no more than 2 screens/routes of your SPA, then you will carry the .js needed for it to work instead of making it load the entire application. Thus also prevents some "super user" from seeing the .js carried beyond what he has access to.

1

Hello!

I use $ocLazyLoad to load third-party scripts and plugins as needed. I even created a dependency mechanism for these plugins to download resolves route. For the files of angular controllers, services, Components... I do not use $ocLazyLoad but minimize and sort them by modules. Option question, no problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.