Inline `ng-template` not found by `ng-include`

孤者浪人 提交于 2019-12-05 06:34:16

<div ng-app>
  <script type="text/ng-template" id="stringCondition">
     Yes, yes it did.
  </script>
  Did it work? <ng-include src="'stringCondition'"></ng-include>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

I had the same problem and came upon this post. I can now see what is causing this issue so hopefully this is helps anyone else with this issue.

The reason rgthree's code works is that the script tag for text/ng-template has to live within the scope of ng-app. So it is fine to have the template in a seperate .js file but just make sure when it is rendered on the HTML page it is within ng-app, otherwise angular won't be aware of it and will attempt to load it from the server.

Seems to work fine for me. Perhaps you have something else going on outside of your example code.

<div ng-app>
  <script type="text/ng-template" id="stringCondition">
     Yes, yes it did.
  </script>
  Did it work? <ng-include src="'stringCondition'"></ng-include>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!