Javascript 在带有browserify的angular中延迟加载组件

Javascript 在带有browserify的angular中延迟加载组件,javascript,angularjs,browserify,Javascript,Angularjs,Browserify,我正在概述一个基于angular的相当复杂的应用程序的体系结构。所以我从angular seed项目开始,这似乎是一个很好的起点。让我烦恼的是,angular应用程序本质上涉及到预先加载所有内容。对于脚本加载程序,似乎没有一种干净的方法 我来自backbone.js的背景,在那里使用require.js进行基于路由器回调的延迟加载非常简单。在某些情况下,角度布线的定义如下所示: // Declare app level module which depends on views, and com

我正在概述一个基于angular的相当复杂的应用程序的体系结构。所以我从angular seed项目开始,这似乎是一个很好的起点。让我烦恼的是,angular应用程序本质上涉及到预先加载所有内容。对于脚本加载程序,似乎没有一种干净的方法

我来自backbone.js的背景,在那里使用require.js进行基于路由器回调的延迟加载非常简单。在某些情况下,角度布线的定义如下所示:

// Declare app level module which depends on views, and components
angular.module('myApp', [
  'ngRoute'
]).
config(['$routeProvider', function($routeProvider) {
  $routeProvider.when({templateURL:'../tmpl.html',controller:'view1Ctrl'})
.when({templateURL:'../tmpl.html',controller:'view1Ctrl'})
.otherwise({redirectTo: '/view1'});
}]);
现在在这里,
$routeProvider.when({templateURL:'../tmpl.html',controller:'view1Ctrl'})
我想延迟加载控制器和模板。我很想用这样的东西:

$routeProvider.when({templateURL:'../tmpl.html',controller:require('view1Ctrl'})
使用browserify,但它似乎不干净,甚至不符合要求。我知道这个问题已经被问了好几次了,但我还没有找到一个明确的答案


我的首选是使用browserify,因为它支持浏览器中备受喜爱的cjs模块。

我不知道如何使用browserify实现这一点,因为我自己从未尝试过,但我强烈建议您仔细研究

作为一个独立的服务,它可以通过加载文件(json、css、js、模板——你可以随意命名)并将其注入到已经运行的angular应用程序中来实现奇迹般的效果

尽管如此,它与路由器(默认的角度路由器或ui路由器)结合使用(imo)效果更好

有一些“种子项目”展示了如何将ocLazyLoad与SystemJS结合使用


但你甚至不需要它。

如果使用ui router、ui router extras和ocLazyLoad,您可以将以下内容组合到延迟加载状态:

main.js

/** 
 * Inject the needed dependencies into our main module.
 */
var main = angular.module('main', [ 'ui.router', 'ct.ui.router.extras.future', 'oc.lazyLoad' ]);

/**
 * Define the lazy loaded states.
 */
main.constant('lazyLoadedStates', [
  {
    name: 'about',
    url:  '/about',
    type: 'lazy',
    src: [
      '/path/to/about.module.js',
      '/path/to/AboutController.js'
    ]
  }
]);

/**
 * Setup the behaviour for when we hit a futureState with the 'lazy'
 * type. 
 * 
 * 1. Setup a deferred object.
 * 2. Resolve the promise when every file defined in the futureState.src has been loaded.
 * 3. Return the promise.
 */
main.config(function ($futureStateProvider, lazyLoadedStates) {
  $futureStateProvider.stateFactory('lazy', function ($q, $ocLazyLoad, futureState) {
    var deferred = $q.defer();

    $ocLazyLoad.load(futureState.src).then(function () {
      deferred.resolve();
    });

    return deferred.promise;
  });

  lazyLoadedStates.forEach($futureStateProvider.futureState);
});
/** 
 * Setup the _real_ '/about' state in this lazy loaded file.
 */
angular.module('about', []).config(function ($stateProvider) {
  $stateProvider.state('about', {
    url: '/about',
    controller: 'AboutController',
    template: 'some_template.html'
  });
});
/** 
 * Register the AboutController in a lazy loaded file. This could be done in about.module.js aswell,
 * but we'll do it here to separate stuff and showcase loading of multiple files. 
 */
angular.module('about').controller('AboutController', function ($state) {
  console.log('Im on a lazy loaded state!', $state.current);
});

这就是“框架”——现在您只需要不断添加更多模块和更多代码,并在
lazyLoadedStates
常量中将真实状态定义与虚拟状态定义相匹配

关于.module.js

/** 
 * Inject the needed dependencies into our main module.
 */
var main = angular.module('main', [ 'ui.router', 'ct.ui.router.extras.future', 'oc.lazyLoad' ]);

/**
 * Define the lazy loaded states.
 */
main.constant('lazyLoadedStates', [
  {
    name: 'about',
    url:  '/about',
    type: 'lazy',
    src: [
      '/path/to/about.module.js',
      '/path/to/AboutController.js'
    ]
  }
]);

/**
 * Setup the behaviour for when we hit a futureState with the 'lazy'
 * type. 
 * 
 * 1. Setup a deferred object.
 * 2. Resolve the promise when every file defined in the futureState.src has been loaded.
 * 3. Return the promise.
 */
main.config(function ($futureStateProvider, lazyLoadedStates) {
  $futureStateProvider.stateFactory('lazy', function ($q, $ocLazyLoad, futureState) {
    var deferred = $q.defer();

    $ocLazyLoad.load(futureState.src).then(function () {
      deferred.resolve();
    });

    return deferred.promise;
  });

  lazyLoadedStates.forEach($futureStateProvider.futureState);
});
/** 
 * Setup the _real_ '/about' state in this lazy loaded file.
 */
angular.module('about', []).config(function ($stateProvider) {
  $stateProvider.state('about', {
    url: '/about',
    controller: 'AboutController',
    template: 'some_template.html'
  });
});
/** 
 * Register the AboutController in a lazy loaded file. This could be done in about.module.js aswell,
 * but we'll do it here to separate stuff and showcase loading of multiple files. 
 */
angular.module('about').controller('AboutController', function ($state) {
  console.log('Im on a lazy loaded state!', $state.current);
});
关于controller.js

/** 
 * Inject the needed dependencies into our main module.
 */
var main = angular.module('main', [ 'ui.router', 'ct.ui.router.extras.future', 'oc.lazyLoad' ]);

/**
 * Define the lazy loaded states.
 */
main.constant('lazyLoadedStates', [
  {
    name: 'about',
    url:  '/about',
    type: 'lazy',
    src: [
      '/path/to/about.module.js',
      '/path/to/AboutController.js'
    ]
  }
]);

/**
 * Setup the behaviour for when we hit a futureState with the 'lazy'
 * type. 
 * 
 * 1. Setup a deferred object.
 * 2. Resolve the promise when every file defined in the futureState.src has been loaded.
 * 3. Return the promise.
 */
main.config(function ($futureStateProvider, lazyLoadedStates) {
  $futureStateProvider.stateFactory('lazy', function ($q, $ocLazyLoad, futureState) {
    var deferred = $q.defer();

    $ocLazyLoad.load(futureState.src).then(function () {
      deferred.resolve();
    });

    return deferred.promise;
  });

  lazyLoadedStates.forEach($futureStateProvider.futureState);
});
/** 
 * Setup the _real_ '/about' state in this lazy loaded file.
 */
angular.module('about', []).config(function ($stateProvider) {
  $stateProvider.state('about', {
    url: '/about',
    controller: 'AboutController',
    template: 'some_template.html'
  });
});
/** 
 * Register the AboutController in a lazy loaded file. This could be done in about.module.js aswell,
 * but we'll do it here to separate stuff and showcase loading of multiple files. 
 */
angular.module('about').controller('AboutController', function ($state) {
  console.log('Im on a lazy loaded state!', $state.current);
});

我希望这能让您大致了解如何在Angular中设置延迟加载状态。我还没有找到一个更简单的方法来做到这一点,但我相信有一些文章是关于如何将ui路由器(或默认的角度路由器)与其他一些“惰性加载程序”耦合的

文档链接:


正如您所说,由于Angular.js的特性,有一种“非黑客方式”可以做到这一点。您必须公开
$controllerProvider.register
$provide.factory
$compileProvider.directive
在某些时候必须在运行状态下使用它们。但是,这是一种黑客行为(此外,我认为这与: