0

使用 angularjs 路由,有时我得到

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

但是我声明了 template/home.html inline

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

出乎意料的是,我不是每次都得到 404,但有时。

我已经读过这个 stackoverflow,他们记得这个规则 https://docs.angularjs.org/api/ng/service/ $templateCache

注意:包含模板的脚本标签不需要包含在文档的头部,但它必须是$rootElement(IE,具有ng-app属性的元素)的后代,否则模板将被忽略。

我已经声明了没有 ng-app 的 angularjs 应用程序,但是通过引导程序

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

所以我的 $rootElement 是文档。事实上,我不会每次都收到 404 错误。是否存在一些竞争条件:有时在使用我的内联模板设置 $templateCache 之前设置了 $routerProvider。

我正在使用requirejs。这可能是原因吗?

有什么帮助吗?

4

1 回答 1

1

我认为您可能会不小心将 ng-template 脚本放在定义主要 Angular 应用程序的标记之外。

<body ng-app="myApp">

  // Your Template shuld stay here 

  <script type="text/ng-template" id="template/home.html">
   ...template stuff...
  </script>
</body>
于 2017-01-26T18:11:43.943 回答