如何使用$ templatecache.put()加载远程模板

veg*_*033 2 caching angularjs 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');
  alert($templateCache.get('tpl1.html'));
  alert($templateCache.get('tpl2.html'));
}); 
Run Code Online (Sandbox Code Playgroud)

当我的代码返回tpl1的HTML代码时,正在返回tpl2的路径。我的问题是:如何使用$ templatecache.put()加载远程模板。

谢谢您的帮助。

Lit*_*rvi 6

尝试调用远程模板并在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');
    });
}); 
Run Code Online (Sandbox Code Playgroud)

造成这种情况的主要原因是,angularjs的templateCache仅接收字符串值,而不像指令上那样,您可以在其中拥有templateUrl。

是此ng-service的文档