ocombe / ocLazyLoad

Lazy load modules & components in AngularJS
https://oclazyload.readme.io
MIT License
2.63k stars 510 forks source link

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

Open hani647 opened 7 years ago

hani647 commented 7 years ago

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("/");
  }
})();
jeevasusej commented 7 years ago

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;
              }]