Как использовать файл для кеша $template?

Мне нужно использовать шаблон из файла HTML.

Поэтому я добавил скрипт, где id - это путь к моему html-файлу.

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

и получить шаблон

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

но шаблон пуст.

PS Если я добавлю шаблон внутри тега скрипта, это работает.

2 ответа

Попробуй это

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

Добавление через сервис $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')
Другие вопросы по тегам