Javascript AngularJS 1.2.0:惰性负载控制器和模板
我是AngularJS的新手。我使用以下代码来实现延迟加载依赖 这在1.0.7中运行得很好,但是延迟承诺无法在1.2.0版本中加载temaplate 在Javascript AngularJS 1.2.0:惰性负载控制器和模板,javascript,angularjs,requirejs,angularjs-routing,Javascript,Angularjs,Requirejs,Angularjs Routing,我是AngularJS的新手。我使用以下代码来实现延迟加载依赖 这在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. *
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;
}
谢谢你的帮助。提前谢谢 目前尚不清楚您的html
是如何加载的,但如果问题与“visualforce页面不支持html扩展”有关,您可以在现有html文件中使用
标记实现模板,如下所示:
<script id="views/view1.html" type="text/ng-template">
<h1>View1</h1>
<div>View Message: {{message}}</div>
<a href="#/home">Home</a>
</script>
视图1
查看消息:{{Message}
ng模板的角度文档
:
这是一个plunker,模板在index.html
中编码:
上面的Plunker使用angularAMD
来简化RequireJS的使用。更多信息请访问:
您的浏览器的开发控制台中是否有错误?@jpmorin-no,它是干净的。但是当我操控“html”时,它会在页面加载时显示undefined,而在导航页面时,它会返回正确的html。我也遇到了同样的问题,我解决了。希望这能有所帮助。谢谢你的回复,我在一些博客上看到过。在routeProvider$routeProvider.when(path,{templateUrl:'path',controller:'path'})中,我们只能使用templateUrl
或templateUrl
@aravindtrue。我想说的是,您可以在$routeProvider.when中使用templateUrl
,并将“path”设置为您创建的
标记的“id”。Angular将能够使用
标记拾取模板。这在angular的网站上我提供的链接中有记录。
<script id="views/view1.html" type="text/ng-template">
<h1>View1</h1>
<div>View Message: {{message}}</div>
<a href="#/home">Home</a>
</script>