简体   繁体   中英

AngularJS unable to get template from $templateCache

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

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="angular.js@1.2.16" 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>");
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM