ocombe/ocLazyLoad

The controller with the name 'StaticPageController' is not registered.

hani647 opened this issue · 1 comments

I've been tring to use ocLazyLoad but this works fine when controller file is cached. I mean for the first visit it gives me controller undefined error but when I visit second time it works fine. Please help

(function() {
  'use strict';

  angular
    .module('abc.config', ['ui.router', 'ngRoute', 'oc.lazyLoad'])
    .config(config);
  // safe dependency injection
  // this prevents minification issues
  config.$inject = ['$routeProvider', '$locationProvider'];
  // run.$inject = [];

  /**
   * App routing
   *
   * You can leave it here in the config section or take it out
   * into separate file
   *
   */
  function config($routeProvider, $locationProvider) {

    // routes
    $routeProvider
      .when('/contact-us', {
        templateUrl: 'app/staticPages/contact-us.html',
        controller: 'StaticPageController',
        controllerAs: 'StaticController',
        resolve: { // Any property in resolve should return a promise and is executed before the view is loaded
          loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
            // you can lazy load files for an existing module
            return $ocLazyLoad.load('app/staticPages/static.controller.js');
          }]
        }
      })
      .otherwise({
        redirectTo: '/404'
      });
    $locationProvider.html5Mode(true);
    // $urlRouterProvider.otherwise("/");
  }
})();

Use like the follow, you will not get the error.

loadMyCtrl: ['$ocLazyLoad', '$q', function ($ocLazyLoad, $q) {
                  
                  var deferred = $q.defer();
                  try {
                      $ocLazyLoad.load('app/staticPages/static.controller.js').then(function () {
                          deferred.resolve();
                      });
                  } catch (ex) {
                      deferred.reject(ex);
                  }

                  return deferred.promise;
              }]