0

Using angularjs routing, sometimes I get

 template/home.html Failed to load resource: 
                    the server responded with a status of 404 (Not Found)

But I declared template/home.html inline

<script type="text/ng-template" id="template/home.html">
   ...template stuff...
</script>

Unexpectedly I don't get 404 every times, but sometimes.

I've already read this stackoverflow, where they remember this rule https://docs.angularjs.org/api/ng/service/$templateCache

Note: the script tag containing the template does not need to be included in the head of the document, but it must be a descendent of the $rootElement (IE, element with ng-app attribute), otherwise the template will be ignored.

I've declared my angularjs app without ng-app, but through bootstrap

angular.bootstrap(document, ['myapp']);

So my $rootElement is the document. Indeed I don't get the 404 errors every times. Is there some race conditions: sometimes $routerProvider is set before $templateCache is set with my inline template.

I'm using requirejs. Could it be the cause?

Any helps?

Community
  • 1
  • 1
Fabio Formosa
  • 904
  • 8
  • 27

1 Answers1

1

I think you may accidentally put your ng-template script outside of the tag where you define your main angular app.

<body ng-app="myApp">

  // Your Template shuld stay here 

  <script type="text/ng-template" id="template/home.html">
   ...template stuff...
  </script>
</body>
  • As I've just written in my question, I've declared my angularjs app without ng-app, but through bootstrap angular.bootstrap(document, ['myapp']); – Fabio Formosa Jan 26 '17 at 18:22
  • sorry I missed that part. have you tried using ng-app maybe it narrows the source of the problem – Salih Şenol Çakarcı Jan 26 '17 at 18:26
  • I need more control over initialization process so I'm using manual bootstrap. I can't add ng-app, because angularjs says "you should not use the ng-app directive when manually bootstrapping your app" – Fabio Formosa Jan 26 '17 at 19:59