Javascript 自定义指令的演示

Javascript 自定义指令的演示,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我正试图让一个自定义指令以angular方式工作,部分遵循 在Chrome中,我只看到一个空白页 F12显示此错误: XMLHttpRequest cannot load file:///home/jeff/workspace/angularjs-intro/playground/my-customer.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-e

我正试图让一个自定义指令以angular方式工作,部分遵循

在Chrome中,我只看到一个空白页

F12显示此错误:

XMLHttpRequest cannot load file:///home/jeff/workspace/angularjs-intro/playground/my-customer.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
以下是我目前得到的信息:

(函数(){
var app=angular.module('docsrestricdirective',[]);
app.controller('controller',function(){
});
应用程序指令('myCustomer',函数(){
返回{
限制:'E',
templateUrl:'my customer.html'
};
});
})();

示例-示例-示例14-生产

您应该在某台服务器上运行代码,就像在没有服务器的情况下运行一样。浏览器尝试加载页面,但url不属于同一域。因此,您将得到声明的错误

了解:

出于众所周知的安全原因,从脚本内部发起的跨站点HTTP请求受到众所周知的限制。例如,使用XMLHttpRequest对象发出的HTTP请求遵循相同的源策略。特别是,这意味着使用XMLHttpRequest的web应用程序只能向加载它的域发出HTTP请求,而不能向其他域发出HTTP请求。开发人员表示希望安全地发展诸如XMLHttpRequest之类的功能,以提出跨站点请求,从而在web应用程序中实现更好、更安全的mashup


注意:在chrome中有禁用安全性的方法,但不是推荐的方法。

我就是这样让它工作的:

script.js:(“传递到指令的数据”)

test.html中

<div ng-controller="Controller as ctrl">
<my-customer></my-customer>

在my-customer.HTML周围需要一个div。此外,我没有看到任何数据被传递到指令。请在服务器上运行代码。正在抛出跨源请求错误。我已回答。这有助于您理解问题和建议的解决方案吗?@A.J感谢您的回答。禁用安全性确实有助于获得正确答案。@Twenty40感谢“数据被传递到指令”的指针。我在我的回答中提供了一个工作样本。谢谢你的回答。我在本地开发中使用了。我在禁用安全模式中遇到了其他问题,所以我决定使用它来启动服务器。这个问题就消失了。
<div ng-controller="Controller as ctrl">
<my-customer></my-customer>
Name: {{ctrl.customer.name}} Address: {{ctrl.customer.address}}