2

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

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

这在 1.0.7 中效果很好,但是延迟承诺无法在 1.2.0 版本中加载模板。

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;

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

4

1 回答 1

3

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

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

Angular 文档ng-template
http ://docs.angularjs.org/api/ng.directive:script

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

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

于 2013-10-13T14:41:30.047 回答