How use file for $template cache?

872 views Asked by At

I need use template from html file.

So I added script where the id is path to my html file.

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

and get template

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

but template is empty.

p.s If I add template inside script tag it is works.

2

There are 2 answers

0
Atul Pandya On

Adding via the $templateCache service:

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

To retrieve the template later, simply use it in your HTML:

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

or get it via Javascript:

$templateCache.get('templateId.html')
0
Chintan Mirani On

Try this

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