首页 > Web开发 > 详细

AngularJs配置路由延迟加载文件

时间:2016-01-15 10:57:14      阅读:341      评论:0      收藏:0      [点我收藏+]

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

 

AngularJs配置路由延迟加载文件

原文:http://www.cnblogs.com/sssleon/p/5132605.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!