AngularJS: lazy loading controllers and content

后端 未结 9 859
小鲜肉
小鲜肉 2020-12-02 19:00

In this simplified scenario, I have two files: index.htm, lazy.htm.

index.htm:

var myApp = angular.module(\'myApp\', []);
myApp.controller(\'embed\',         


        
9条回答
  •  无人及你
    2020-12-02 19:39

    I am sending you sample code. It is working fine for me. So please check this:

    var myapp = angular.module('myapp', ['ngRoute']);
    
    /* Module Creation */
    var app = angular.module('app', ['ngRoute']);
    
    app.config(['$routeProvider', '$controllerProvider', function ($routeProvider, $controllerProvider) {
    
    app.register = {
        controller: $controllerProvider.register,
        //directive: $compileProvider.directive,
        //filter: $filterProvider.register,
        //factory: $provide.factory,
        //service: $provide.service
    };
    
    
    //    so I keep a reference from when I ran my module config
    function registerController(moduleName, controllerName) {
        // Here I cannot get the controller function directly so I
        // need to loop through the module's _invokeQueue to get it
        var queue = angular.module(moduleName)._invokeQueue;
        for (var i = 0; i < queue.length; i++) {
            var call = queue[i];
            if (call[0] == "$controllerProvider" &&
               call[1] == "register" &&
               call[2][0] == controllerName) {
                app.register.controller(controllerName, call[2][1]);
            }
        }
    }
    
    
    var tt = {
        loadScript:
    function (path) {
        var result = $.Deferred(),
        script = document.createElement("script");
        script.async = "async";
        script.type = "text/javascript";
        script.src = path;
        script.onload = script.onreadystatechange = function (_, isAbort) {
            if (!script.readyState || /loaded|complete/.test(script.readyState)) {
                if (isAbort)
                    result.reject();
                else {
                    result.resolve();
                }
            }
        };
        script.onerror = function () { result.reject(); };
        document.querySelector(".shubham").appendChild(script);
        return result.promise();
    }
    }
    
    function stripScripts(s) {
        var div = document.querySelector(".shubham");
        div.innerHTML = s;
        var scripts = div.getElementsByTagName('script');
        var i = scripts.length;
        while (i--) {
            scripts[i].parentNode.removeChild(scripts[i]);
        }
        return div.innerHTML;
    }
    
    
    function loader(arrayName) {
        return {
            load: function ($q) {
                stripScripts(''); // This Function Remove javascript from Local
                var deferred = $q.defer(),
                map = arrayName.map(function (obj) {
                    return tt.loadScript(obj.path)
                    .then(function () {
                        registerController(obj.module, obj.controller);
                    })
                });
    
                $q.all(map).then(function (r) {
                    deferred.resolve();
                });
                return deferred.promise;
            }
        };
    };
    
    
    
    $routeProvider
        .when('/first', {
            templateUrl: '/Views/foo.html',
            resolve: loader([{ controller: 'FirstController', path: '/MyScripts/FirstController.js', module: 'app' },
                { controller: 'SecondController', path: '/MyScripts/SecondController.js', module: 'app' }])
        })
    
        .when('/second', {
            templateUrl: '/Views/bar.html',
            resolve: loader([{ controller: 'SecondController', path: '/MyScripts/SecondController.js', module: 'app' },
            { controller: 'A', path: '/MyScripts/anotherModuleController.js', module: 'myapp' }])
        })
        .otherwise({
            redirectTo: document.location.pathname
            });
    }])
    

    And in HTML Page:

    
    
    
    

    Thank U

提交回复
热议问题