AngularJS 1.2.0:延迟加载控制器和模板

时间:2013-10-12 10:12:03

标签: javascript angularjs requirejs angularjs-routing

我是AngularJS的新手。我使用以下代码来实现延迟加载依赖性。

https://github.com/matys84pl/angularjs-requirejs-lazy-controllers/

这在1.0.7中运行良好,但延迟承诺未能在1.2.0版本中加载temaplate。

js/lib/utils/route-config.js

中的

routeDefinition.template = function () {
            /*
             *  Here is the problem, 'html' is undefined on page load. 
             *  But if we click the menu navigation, the 'html' is getting loaded. 
             *  I am not sure how to achieve promise for loading template in RequireJS.
             */
            return html;
        };
        routeDefinition.controller = controllerName;


routeDefinition.resolve = {
        delay:function ($q, $rootScope) {
            defer = $q.defer();
            if (!html) {
                var dependencies = [controllerName, "text!" + templateUrl];
                if (directives) {
                    dependencies = dependencies.concat(directives);
                }
                require(dependencies, function () {
                    var controller = arguments[0],
                        template = arguments[1];

                    for (var i = 2; i < arguments.length; i++) {
                        lazyDirectives.register(arguments[i]);
                    }

                    $controllerProvider.register(controllerName, controller);
                    html = template;
                    defer.resolve();
                    $rootScope.$apply()
                })

            } else {
                defer.resolve();
            }
            return defer.promise;
        }

我已经使用下面的代码更改了代码,即我没有使用template,而是将其更改为templateUrl并且它完美无缺。由于我在salesforce.com上实现它并且visualforce页面不支持html扩展,我不想使用templateUrl

routeDefinition.templateUrl = 'js/'+templateUrl;
        routeDefinition.controller = controllerName;
        routeDefinition.resolve = {
            delay:function ($q, $rootScope, $http) {
                defer = $q.defer();
                if (!html) {
                    //var dependencies = ["lib/text!" + templateUrl, controllerName];
                    var dependencies = [controllerName];
                    if (routeDependends) {
                        dependencies = dependencies.concat(routeDependends.directives);
                        dependencies = dependencies.concat(routeDependends.services);
                        dependencies = dependencies.concat(routeDependends.filters);
                    }

                    require(dependencies, function () {
                        var controller = arguments[0], 
                        //template = arguments[0],
                        incCnt = 1, 
                        directiveCnt = (routeDependends.directives.length+incCnt),
                        serviceCnt = (routeDependends.directives.length+routeDependends.services.length+incCnt),
                        filterCnt = (routeDependends.directives.length+routeDependends.services.length+routeDependends.filters.length+incCnt);

                        $controllerProvider.register(controllerName, controller);

                        if(routeDependends.directives.length > 0)
                            for (var i = incCnt; i < directiveCnt; i++) { onDemandDependencies.registerDirectives(arguments[i]); }
                        if(routeDependends.services.length > 0)
                            for (var i = directiveCnt; i < serviceCnt; i++) { onDemandDependencies.registerServices(arguments[i]); }
                        if(routeDependends.filters.length > 0)
                            for (var i = serviceCnt; i < filterCnt; i++) { onDemandDependencies.registerFilters(arguments[i]); }

                        //html = template;
                        defer.resolve();
                        $rootScope.$apply()
                    });
                } else {
                    defer.resolve();
                }
                return defer.promise;
            }
        }
        console.log(routeDefinition);
        return routeDefinition;

任何帮助表示赞赏。提前致谢。

1 个答案:

答案 0 :(得分:3)

目前尚不清楚您的html是如何加载的,但如果问题与“visualforce页面不支持html扩展”相关,则可以使用现有html中的<script>标记实现模板文件为:

<script id="views/view1.html" type="text/ng-template">
    <h1>View1</h1>
    <div>View Message: {{message}}</div>
    <a href="#/home">Home</a>
</script>

ng-template的角度文档:
http://docs.angularjs.org/api/ng.directive:script

这是一个用index.html编码模板的plunker:
http://plnkr.co/edit/TaTl5rtpxuq67roDcOss?p=preview

上面的Plunker使用angularAMD来简化RequireJS的使用。更多信息:
http://marcoslin.github.io/angularAMD/