1
votes

I am able to lazy load angularjs with the help of requirejs. But, how can I load modules that needs to be associated to the controller?

My example configuration in app.js looks like the following, loading all the providers and keeping a reference.

var app = angular.module('myApp', ['ui.router'])

var cacheProviders = {};

    app.getProvider = function () {
        return cacheProviders.$provide;
    }

    app.getCompileProvider = function () {
        return cacheProviders.$compileProvider;
    }

    app.getControllerProvider = function () {
        return cacheProviders.$controllerProvider;
    }

    app.getFilterProvider = function () {
        return cacheProviders.$filterProvider;
    }

app.config(['$stateProvider', '$urlRouterProvider', '$controllerProvider', '$compileProvider', '$filterProvider', '$provide',
            function ($stateProvider, $urlRouterProvider, $controllerProvider, $compileProvider, $filterProvider, $provide) {

            (function () {
                cacheProviders.$controllerProvider = $controllerProvider;
                cacheProviders.$compileProvider = $compileProvider;
                cacheProviders.$filterProvider = $filterProvider;
                cacheProviders.$provide = $provide;
            })();

            var lazyCtrlLoad = function (controllerName) {
                return ["$q", function ($q) {
                    var deferred = $q.defer();
                    require([controllerName], function () {
                        deferred.resolve();
                    });
                    return deferred.promise;
                }];
            }
             $stateProvider.state('main.view2b', {
                    url: '/view2b',
                    templateUrl: 'forms/empl/searchEmplForm.html',
                    controllerAs: 'srchC',
                    controller: 'searchEmplCtrl',
                    resolve: {
                        loadOtherCtrl: lazyCtrlLoad('searchEmplCtrl')
                    }
                })

In my other module, I am trying to register controllers, load services..

define([
        'angular', 'angularResource'
    ], function (angular) {
      angular.module('myApp')
        .getControllerProvider()
        .register(ctrl, ...)

But, while loading service below, I need access to $resource which is part of ngResource module in angularResource.

angular.module('myApp')
    .getProvider().service('deptService', ['$resource', function ($resource) {
        return $resource('/dept/:dept', {dept: '@_dept'});
    }])

How can I load ngResource while initalizing the javascript controllers/services lazily?

1

1 Answers

0
votes

Take a look to AngularAMD here. It allows you to load controllers in the ui-router without using lazyload. This AngularAMD is used to integrate requireJs and Angular.

$stateProvider
  .state('home', {
    url: '',
    views: {
      '@': angularAmd.route({
        templateUrl: 'ngApplication/application/shared/layouts/basic/basicTplView.html',
        controllerUrl: 'ngApplication/application/shared/layouts/basic/basicTplCtrl.js',
        controller: 'basicTplCtrl'
       }),
      'header@home': angularAmd.route({
         templateUrl: 'ngApplication/application/shared/layouts/header/headerView.html',
         controllerUrl: 'ngApplication/application/shared/layouts/header/headerCtrl.js',
         controller: 'headerCtrl'
       })
    },
});

Also, you are using requirejs, you can load all the dependencies for an specific controller using the define syntax of requireJs. Let's say you want to create a loginCtroller in a separately file, and this controller depends on another angular service:

define(['app', 'transformRequestAsFormPostService'], function (app) {
    app.controller('loginCtrl', ['$scope', '$rootScope', '$sce', '$http', '$state', 'transformRequestAsFormPostService', function ($scope, $rootScope, $sce, $http, $state, transformRequestAsFormPost) {

      $scope.login = function () {
        /*do something here using the service*/          
      };
    }]);
});

Here, the dependency called transformRequestAsFormPostService is another file, I defined it in the main.js (requireJs confifguration file) and it's defined using the same approach than the loginCtrol. Now I am using it in my project and its working so far so good.

Regards,

Ernesto