• angularJs按需加载代码(未验证)


    一网友写的AngularJs按需加载代码,但未验证,放着备用。
     
    application.config(
                   function($routeProvider) {
                     $routeProvider.
                        when('/:module/:view', {
                            templateUrl: function(params) {
                                var core = angular.element(document).injector().get('core');
                                core.openView(params.module, params.view);
                                return 'app/views/{0}/{1}.jsp'.format(params.module, params.view);
                            }
                       }).
                       otherwise({
                         redirectTo: '/home/landing'
                       });
                   });
    'use strict';
    // declare an application core module
    var coreModule = angular.module('core', ['network']);

    coreModule.factory('core',
        function (http, $location) {
            return {
                openView: function (module, view) {
                    if (!module) {
                        throw new Error('Please provide module.');
                    }
                    
                    if (!view) {
                        throw new Error('Please provide view.');
                    }
                    
                    http.getController(module, view);
                    if($location.path() !== '/{0}/{1}'.format(module, view)) {
                        $location.path('/{0}/{1}'.format(module, view)).replace();
                    }
                }
            };
        }
    );
    'use strict';
    // declare a network service module
    var httpModule = angular.module('network', []);

    httpModule.factory('http',
        function ($http, $q) {
            return {
                get: function (url) {
                    if (!url) {
                        throw new Error('Please provide request url.');
                    }
                    
                    var defer = $q.defer();
                    
                    $http.get(url).then(
                        function(response) {
                            defer.resolve(response.data);
                        },
                        function(error) {
                            defer.reject(error);
                        });
                    return defer.promise;
                },
                post: function (url, paramObj) {
                    if (!url) {
                        throw new Error('Please provide request url.');
                    }
                    
                    var defer = $q.defer();
                    
                    paramObj = paramObj || {};
                    $http.post(url, paramObj).then(
                        function(response) {
                            defer.resolve(response.data);
                        },
                        function(error) {
                            defer.reject(error);
                        });
                    return defer.promise;
                },
                getController: function (module, controller) {
                    if (!module) {
                        throw new Error('Please provide module.');
                    }
                    
                    if (!controller) {
                        throw new Error('Please provide controller.');
                    }
                    
                    var webRoot = window.location.href.substr(0, window.location.href.lastIndexOf('/home.action'));
                    var url = '{0}/{1}/{2}/{3}.js'.format(webRoot, 'app/controllers', module, controller);
                    var existed = false;
                    $.each($('head').find('script'), function(index, value) {
                     if(value.src === url) {
                     existed = true;
                     }
                    });
                    
                    if(!existed) {
                        var script = document.createElement('script');
                        script.type = 'text/javascript';
                        script.src = url;
                        script.charset = 'utf-8';
                        document.head.appendChild(script);
                    }
                }
            };
        }
    );
  • 相关阅读:
    socket (一)
    yield生成器及字符串的格式化
    python模块(json和pickle模块)
    python标准模块(time、datetime及hashlib模块)
    python标准模块(os及sys模块)
    python模块简介
    python --> 正则表达式
    python --> 递归 以及装饰器
    python基础知识(四)
    python基础知识(三)
  • 原文地址:https://www.cnblogs.com/Rising/p/3606888.html
Copyright © 2020-2023  润新知