AngularJs需要按需求加载文件,需要对route进行重定义
var App= angular.module(‘AppConfigService‘, [‘ui.router‘, ‘oc.lazyLoad‘]); App.provider(‘appConfig‘, [‘$stateProvider‘, ‘$urlRouterProvider‘, ‘$ocLazyLoadProvider‘, function ($stateProvider, $urlRouterProvider, $ocLazyLoadProvider) { this.module = function (modules) { $ocLazyLoadProvider.config({ modules: modules }); } this.otherwise = function (url) { $urlRouterProvider.otherwise(url); return this; } this.state = function (stateObj) { var deps = []; if (stateObj.deps) { deps = stateObj.deps.names; if (!deps) deps = []; if (stateObj.deps.files && stateObj.deps.files.length) deps.push( { name: ‘BayerApp‘, serie: true, insertBefore: ‘#ng_load_plugins_before‘, // load the above css files before ‘#ng_load_plugins_before‘ files: stateObj.deps.files }); } $stateProvider.state(stateObj.name, { url: stateObj.url, templateUrl: stateObj.templateUrl || ‘views/‘ + stateObj.name + ‘.html‘, controller: stateObj.controller, resolve: { loadFiles: [‘$ocLazyLoad‘, function ($ocLazyLoad) { return $ocLazyLoad.load(deps); }] } }) return this; } this.$get = function () { return null; } }])
注入创建的服务,并使用
var AppConfig = angular.module(‘BayerApp‘, [‘AppConfigService‘]); AppConfig.config([‘appConfigProvider‘, function (appConfigProvider) { appConfigProvider.module([]); appConfigProvider .otherwise(‘/single‘) .state({ name: ‘single‘, url: ‘/single‘, templateUrl: ‘spa/views/single.html‘, controller: "SingleController", deps: { names: [], files: [ ‘spa/controllers/singlecontroller.js‘ ] } }) }]);
原文:http://www.cnblogs.com/sssleon/p/5132605.html