我只是在学习Angularjs,以及如何使用templateUrl来加载模板.
我有一个简单的指令:
var mainApp = angular.module("mainApp", []);
mainApp.directive("request", function() {
return {
restrict: "E",
scope: {
data: "@"
},
templateUrl: "te.html"
}
})
Run Code Online (Sandbox Code Playgroud)
我尝试使用这样的指令:
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-2.0.0.js"></script>
<script src="js/angular.js"></script>
<link href="css/bootstrap.css" rel="stylesheet" />
<script src="js/bootstrap.js"></script>
<script src="js/main.js"></script>
<style type="text/css">
div {
background-color: cornflowerblue;
}
#appPanel {
width: 900px;
height: 1000px;
}
</style>
</head>
<body>
<div id="appPanel" ng-app="mainApp" class="container">
<div class="row-fluid" style="height: 15%">
<div class="span12">
title
</div>
</div>
<div class="row-fluid" style="height: 85%">
<div class="span3" style="height: 100%">
actions
</div>
<div class="span9" style="height: 100%" ng-controller="AppCtrl">
<div ng-repeat="request in data.requests">
<request data="request"></request>
</div>
</div>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
打开页面后,我遇到了以下异常:
XMLHttpRequest cannot load file:///Users/chenguangli/Documents/workspace-web/ournotes/te.html. Origin null is not allowed by Access-Control-Allow-Origin. default.html:0
Error: Failed to load template: te.html
at Error (<anonymous>)
at file:///Users/chenguangli/Documents/workspace-web/ournotes/js/angular.js:4503:17
at file:///Users/chenguangli/Documents/workspace-web/ournotes/js/angular.js:8898:11
at wrappedErrback (file:///Users/chenguangli/Documents/workspace-web/ournotes/js/angular.js:6806:57)
at file:///Users/chenguangli/Documents/workspace-web/ournotes/js/angular.js:6882:53
at Object.Scope.$eval (file:///Users/chenguangli/Documents/workspace-web/ournotes/js/angular.js:8011:28)
at Object.Scope.$digest (file:///Users/chenguangli/Documents/workspace-web/ournotes/js/angular.js:7876:25)
at Object.Scope.$apply (file:///Users/chenguangli/Documents/workspace-web/ournotes/js/angular.js:8097:24)
at done (file:///Users/chenguangli/Documents/workspace-web/ournotes/js/angular.js:9111:20)
at completeRequest (file:///Users/chenguangli/Documents/workspace-web/ournotes/js/angular.js:9274:7) js/angular.js:5704
js/angular.js:5704
js/angular.js:4800
wrappedErrback js/angular.js:6808
js/angular.js:6882
Scope.$eval js/angular.js:8011
Scope.$digest js/angular.js:7876
Scope.$apply js/angular.js:8097
done js/angular.js:9111
completeRequest js/angular.js:9274
xhr.onreadystatechange js/angular.js:9244
Run Code Online (Sandbox Code Playgroud)
我没有尝试加载模板文件跨域(te.html与default.html相同).
任何人都可以帮我弄清楚发生了什么事吗?
pko*_*rce 71
问题是您在文件系统上运行示例(使用file://协议),并且许多浏览器(Chrome,Opera)在使用file://协议时限制XHR调用.AngularJS模板通过XHR下载,结合使用file://协议会导致您获得错误.
在解决这种情况时,您有几种选择:
<script>指令在您的index.html文件中嵌入模板:http://docs.angularjs.org/api/ng.directive:script,以便使用主HTML文件下载模板,不再需要通过XHR加载它们file://协议进行XHR调用.例如,这可以在Chrome中完成,例如:允许Google Chrome使用XMLHttpRequest从本地文件加载URLBez*_*ezR 14
如果安装了Node和NPM,请运行:npm install http-server -g
然后导航到包含您的应用程序的文件夹并运行:http-server
我在这篇SO帖子中找到了答案:Quick Node Server
你可以在这里下载Node(NPM自带节点):Node
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
58779 次 |
| 最近记录: |