angularjs – 基于路由组动态加载控制器

可以根据路由组动态加载控制器,js文件和模板吗? Psuedo代码不起作用:
$routeProvider.when('/:plugin',function(plugin) {
  templateUrl: 'plugins/' + plugin + '/index.html',controller: plugin + 'Ctrl',resolve: { /* Load the JS file,from 'plugins/' + plugin + '/controller.js' */ }
});

我看到很多这样的问题,但没有一个加载基于路由组的js文件/控制器.

我设法解决了由@calebboyd,http://ify.io/lazy-loading-in-angularjs/http://weblogs.asp.net/dwahlin/archive/2013/05/22/dynamically-loading-controllers-and-views-with-angularjs-and-requirejs.aspx启发的

使用http://dustindiaz.com/scriptjs

app.js

app.config(function($controllerProvider,$compileProvider,$filterProvider,$provide) {
  app.register = {
    controller: $controllerProvider.register,directive: $compileProvider.directive,filter: $filterProvider.register,factory: $provide.factory,service: $provide.service
  };
});

然后我注册“按组控制器”路由.

$routeProvider.when('/:plugin',{

  templateUrl: function(rd) {
    return 'plugin/' + rd.plugin + '/index.html';
  },resolve: {
    load: function($q,$route,$rootScope) {

      var deferred = $q.defer();

      var dependencies = [
        'plugin/' + $route.current.params.plugin + '/controller.js'
      ];

      $script(dependencies,function () {
        $rootScope.$apply(function() {
          deferred.resolve();
        });
      });

      return deferred.promise;
    }
  }
});

controller.js

app.register.controller('MyPluginCtrl',function ($scope) {
  ...
});

的index.html

<div ng-controller="MyPluginCtrl">
  ...
</div>

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...