最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Inline `ng-template` not found by `ng-include` - Stack Overflow

programmeradmin6浏览0评论

I have a view being loaded with an embedded text/ng-template which is not being found by a ng-include later in the file. The script block sits at the very top of the view file:

<script type="text/ng-template" id="stringCondition">
    <!-- template guts -->
</script>

Which I am loading later in the file with:

<ng-include src="'stringCondition'"></ng-include>

But is producing a 404 error in the console:

GET http://localhost/~me/stringCondition 404 (Not Found)

I've tried several variants on naming (like having .html on the end) and using ng-include as an attribute instead of the high level element. All with no luck.

What could be causing an embedded ng-template to not be registering with an ng-include in the same view file?

UPDATE:

As ments and answers have pointed out, the basic design of my code is correct. But something is causing ng-template to (apparently) fail to make the template available to the system.

I updated my code to pull from a HTML file (instead of an imbedded template) and it works fine.

Is there a mon situation that I could be running into that breaks ng-template?

I have a view being loaded with an embedded text/ng-template which is not being found by a ng-include later in the file. The script block sits at the very top of the view file:

<script type="text/ng-template" id="stringCondition">
    <!-- template guts -->
</script>

Which I am loading later in the file with:

<ng-include src="'stringCondition'"></ng-include>

But is producing a 404 error in the console:

GET http://localhost/~me/stringCondition 404 (Not Found)

I've tried several variants on naming (like having .html on the end) and using ng-include as an attribute instead of the high level element. All with no luck.

What could be causing an embedded ng-template to not be registering with an ng-include in the same view file?

UPDATE:

As ments and answers have pointed out, the basic design of my code is correct. But something is causing ng-template to (apparently) fail to make the template available to the system.

I updated my code to pull from a HTML file (instead of an imbedded template) and it works fine.

Is there a mon situation that I could be running into that breaks ng-template?

Share Improve this question edited Mar 20, 2015 at 18:47 Nicholas Pappas asked Mar 20, 2015 at 17:48 Nicholas PappasNicholas Pappas 10.6k12 gold badges57 silver badges90 bronze badges 2
  • jsfiddle/nozbaveq working for me. So the issue is not this part of code :) – ehlers Commented Mar 20, 2015 at 18:44
  • @ehlers - yup, something weird is going on, see update on getting it working with regular files. Something is breaking ng-template but I'm at a loss. – Nicholas Pappas Commented Mar 20, 2015 at 18:48
Add a ment  | 

2 Answers 2

Reset to default 15

<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./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./ajax/libs/angularjs/1.2.23/angular.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论