AngularJS unable to get template from $templateCache

1.3k views Asked by At

I am not able to get the template snippets from template cache (which looks like the following:)

<script id="sub1.html" type="text/ng-template">
<div> sub1 Content</div></script>

I think it is because of line number 11 in index.html file

<div class="hiddenContent" ng-include="'templates.html'">

If I replace this line with the templates.html it works.

How to make this work keeping the line number 11?

Look at my code in Plunkr Code

1

There are 1 answers

3
Huy Hoang Pham On

It works, try moving the template into the index.html

http://plnkr.co/edit/MOwCD8UyRT2IBmaeeAMX?p=preview

<html ng-app="myApp">

<head>
  <script data-require="[email protected]" data-semver="1.2.16" src="https://code.angularjs.org/1.2.16/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-controller="myCtrl">
  <div class="hiddenContent" ng-include="'templates.html'">
  </div>
  <h1>Hello Plunker!</h1>
  {{test}}
  <myapp-directive></myapp-directive>

  </div>
</body>

</html>

<script id="sub1.html" type="text/ng-template">
  <div>sub1 Content</div>
</script>

<script id="sub2.html" type="text/ng-template">
  <div>sub2 Content</div>
</script>

Maybe the problem is AngularJS can't find the ng-template in another file. It also work if you define the sub1.html in templateCache:

var app=angular.module("myApp",[]);
app.run(["$templateCache", function ($templateCache) {
  $templateCache.put("sub1.html","<div> sub1 Content</div>");
}]);