2

我已经阅读了 $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'));
}); 

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

谢谢您的帮助。

4

1 回答 1

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');
    });
}); 

这样做的主要原因是 angularjs 的 templateCache 只接收字符串值,而不像在指令中那样,例如,您可以在其中拥有一个 templateUrl。

是此 ng-service 的文档

于 2017-02-27T20:58:44.683 回答