【问题标题】:How use file for $template cache?如何使用文件进行 $template 缓存?
【发布时间】:2016-02-23 19:36:59
【问题描述】:

我需要使用 html 文件中的模板。

所以我添加了脚本,其中 id 是我的 html 文件的路径。

<script type="text/ng-template" id="app/directives/userAvatarTooltip.html"><script>

获取模板

var template = $templateCache.get('app/directives/userAvatarTooltip.html');
return template;

但是模板是空的。

p.s 如果我在脚本标签中添加模板,它是有效的。

【问题讨论】:

  • 你想要它在指令中吗?

标签: angularjs angular-templatecache


【解决方案1】:

试试这个

    var clone = $compile($templateCache.get("app/directives/userAvatarTooltip.html"));
    return $scope.$apply(function () {
        return clone($scope);
    });

【讨论】:

    【解决方案2】:

    通过 $templateCache 服务添加:

    var myApp = angular.module('myApp', []);
    myApp.run(function($templateCache) {
      $templateCache.put('templateId.html', 'This is the content of the template');
    });
    

    要稍后检索模板,只需在 HTML 中使用它:

    <div ng-include=" 'templateId.html' "></div>
    

    或通过 Javascript 获取:

    $templateCache.get('templateId.html')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-30
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 2012-04-13
      • 2020-06-23
      • 1970-01-01
      相关资源
      最近更新 更多