我是 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;
任何帮助表示赞赏。提前致谢。