Angularjs 如何使用$templatecache.put()加载远程模板
我已经阅读了$templateCache的文档,但是我仍然很难理解如何使用put函数将远程模板加载到缓存中 以下是一个例子:Angularjs 如何使用$templatecache.put()加载远程模板,angularjs,caching,angular-templatecache,Angularjs,Caching,Angular Templatecache,我已经阅读了$templateCache的文档,但是我仍然很难理解如何使用put函数将远程模板加载到缓存中 以下是一个例子: onBoardingApp.run(function ($templateCache, $http) { $templateCache.put('tpl1.html', '<p>Hello World</p>'); $templateCache.put('tpl2.html', '~/Templates/Pi/pi-1.html');
onBoardingApp.run(function ($templateCache, $http) {
$templateCache.put('tpl1.html', '<p>Hello World</p>');
$templateCache.put('tpl2.html', '~/Templates/Pi/pi-1.html');
alert($templateCache.get('tpl1.html'));
alert($templateCache.get('tpl2.html'));
});
onBoardingApp.run(函数($templateCache,$http){
$templateCache.put('tpl1.html','helloworld);
$templateCache.put('tpl2.html','~/Templates/Pi/Pi-1.html');
警报($templateCache.get('tpl1.html');
警报($templateCache.get('tpl2.html');
});
当我的代码为tpl1返回HTML代码时,为tpl2返回路径。我的问题是:如何使用$templatecache.put()加载远程模板
谢谢您的帮助。尝试调用远程模板并在templateCache上设置它:
onBoardingApp.run(function ($templateCache, $http) {
$templateCache.put('tpl1.html', '<p>Hello World</p>');
console.log($templateCache.get('tpl1.html'));
$http.get('/Templates/Pi/pi-1.html').then(function (response) {
$templateCache.put('tpl2.html', response.data);
console.log($templateCache.get('pl2.html'));
}, function (errorResponse) {
console.log('Cannot load the file template');
});
});
onBoardingApp.run(函数($templateCache,$http){
$templateCache.put('tpl1.html','helloworld);
log($templateCache.get('tpl1.html');
$http.get('/Templates/Pi/Pi-1.html')。然后(函数(响应){
$templateCache.put('tpl2.html',response.data);
log($templateCache.get('pl2.html');
},函数(错误响应){
log('无法加载文件模板');
});
});
这样做的主要原因是angularjs的templateCache只接收字符串值,不像在指令上那样,例如,在指令中可以有一个templateUrl
是此ng服务的文档感谢您的回复。不幸的是,控制台只写第一个模板,而不是第二个模板,所以我假设它没有被加载。另外,我在tpl2的路径中有一个错误,它应该是“/Templates/Pi/Pi-1.html”。很抱歉,我在上面的代码中有一个错误。。。现在应该可以了。此外,还需要确保为模板放置正确的路径。有关加载模板问题的更多信息,可以调试$http函数上的“errorResponse”。实际上,get('pl2.html')中还有另一个打字错误。但代码是有效的。谢谢你的帮助。