【问题标题】:ng-include not working for Chromeng-include 不适用于 Chrome
【发布时间】:2016-11-13 18:32:22
【问题描述】:

我创建了一个 test.html 文件来尝试调试这个问题。

test.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
    <script src="app/controllers/MainController.js"></script>
  </head>
  <body ng-app="MyApp" ng-controller="MyController">
    <h1>{{ siteHeader }}</h1>
    <div ng-include="'assets/templates/header.html'"></div>
  </body>
</html>

header.html(资产/模板内部)

<div class="jumbotron">
  <img id="logo" src="assets/img/logo.png" />
  <br />
  <h1>{{ siteHeader }}</h1>
</div>

当我在 Chrome 中打开 index.html 时,第一个 {{ siteHeader }} 将加载网站的标题。 header.html 的任何内容(siteHeader 和徽标 PNG)都不会在其下方加载。当我在 Firefox 或 Safari 中打开相同的 index.html 文件时,header.html 内容确实出现在第一个 {{ siteHeader }} 下方。

我做错了什么/错过了什么?

【问题讨论】:

  • 在您的问题中附加 js 脚本,以便更好地分析
  • 您包含的唯一 js 文件是 MainController.js。是否包含 MyApp 的定义?你能发布控制器吗?

标签: javascript angularjs


【解决方案1】:

我假设您从本地硬盘驱动器运行该文件。 (file://)。 Chrome 阻止脚本从 file 源读取文件。这就是为什么 Angular 不能成功读取模板文件并将其包含到页面中。

您有 2 个解决方案:最简单的一个是在 allow file access 模式下运行 chrome。这可以帮助你:How to launch html using Chrome at "--allow-file-access-from-files" mode?


另一个解决方案是创建一个简单的 http 本地服务器,并从该服务器运行站点。您可以使用 php、nodejs、ruby、python 或其他任何方式运行此服务器。这可以帮助你:Run Local Server


更多信息:

【讨论】:

  • 谢谢,这正是我所需要的。我在 Mac 上,终端打开 /Applications/Google\ Chrome.app --args --allow-file-access-from-files 效果很好。
  • 不要这样做!你打开你的机器攻击。而是运行本地服务器。那面旗帜很危险!!使您的文件系统保持打开状态以供访问。默认情况下,来自任何地方(本地或网络)的文档不应具有对本地 file:/// 资源的任何访问权限。
【解决方案2】:

直接从硬盘运行时,Angular 将无法工作。您应该尝试使用Brackets editor。它使用Live Development,通过它您可以轻松地渲染 Angular 应用程序,而无需运行额外的服务器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-29
    • 2016-12-24
    • 2016-06-15
    • 2015-05-13
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多