产品-color.html
<div class="list-group-item">
<h3>Hello <em class="pull-right">Brother</em></h3>
</div>
应用程序.js
(function() {
var app = angular.module('gemStore', []);
app.controller('StoreController', function($http){
this.products = gem;
}
);
app.directive('productColor', function() {
return {
restrict: 'E', //Element Directive
templateUrl: 'product-color.html'
};
}
);
var gem = [
{
name: "Shirt",
price: 23.11,
color: "Blue"
},
{
name: "Jeans",
price: 5.09,
color: "Red"
}
];
})();
一旦我使用名为 productColor 的自定义指令输入 product-color.html 的包含,我就开始收到此错误:
XMLHttpRequest cannot load file:///C:/product-color.html. Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource.
angular.js:11594 Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///C:/product-color.html'.
可能出什么问题了?这是 product-color.html 的路径问题吗?
我所有的三个文件都在同一个根文件夹中 C:/user/project/
发生此错误是因为您只是直接从浏览器打开 html 文档。要解决此问题,您需要从网络服务器提供您的代码并在本地主机上访问它。如果您有 Apache 设置,请使用它来提供您的文件。一些 IDE 内置了 Web 服务器,例如 JetBrains IDE、Eclipse...
如果您有 Node.Js 设置,那么您可以使用http-server。只需运行npm install http-server -g
,您就可以在像http-server C:\location\to\app
.
非常简单的修复
在终端
$ cd yourAngularApp
~/yourAngularApp $ python -m SimpleHTTPServer
现在,在浏览器中访问 localhost:8000,页面将显示
在 chrome 中不允许该操作。您可以使用 HTTP 服务器 (Tomcat) 或使用 Firefox。
我的问题只是通过添加http://
到我的 url 地址来解决。例如我用http://localhost:3000/movies
而不是localhost:3000/movies
.
如果您在 chrome/chromium 浏览器中使用它(例如:在 Ubuntu 14.04 中),您可以使用以下命令之一来解决此问题。
ThinkPad-T430:~$ google-chrome --allow-file-access-from-files
ThinkPad-T430:~$ google-chrome --allow-file-access-from-files fileName.html
ThinkPad-T430:~$ chromium-browser --allow-file-access-from-files
ThinkPad-T430:~$ chromium-browser --allow-file-access-from-files fileName.html
这将允许您以铬或铬加载文件。如果您必须对 Windows 执行相同的操作,您可以在 chrome 快捷方式的属性中添加此开关或cmd
使用标志运行它。Chrome、Opera、Internet Explorer 默认不允许此操作。默认情况下,它仅适用于 Firefox 和 safari。因此,使用此命令将对您有所帮助。
或者,您也可以根据您熟悉的语言将其托管在任何 Web 服务器(例如:Tomcat-java、NodeJS-JS、Tornado-Python 等)上。这将适用于任何浏览器。