AngularJS 未找到指令外部模板
AngularJS Directive External Template Not Found
我正在尝试创建一组可重复使用的小部件,这些小部件可以在我创建的任何网站上使用。 AngularJS 指令似乎是解决此问题的好方法,但我在将它们与外部模板文件一起使用时遇到了问题。
如果我对指令使用内联模板,它加载得很好,但如果我使用外部模板,它会抛出错误:
https://docs.angularjs.org/error/$compile/tplrt?p0=bwInfoCard&p1=
这是我的测试夹具:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Directive Test Fixture</title>
<link rel="stylesheet" href="./Style.css" type="text/css" />
<script src="../../Libraries/angular.min.js"></script>
<script src="./Widget.js"></script>
<script src="./Fixture.js"></script>
</head>
<body ng-app="BaseWidgetFixtures">
<h1>Base Info Card</h1>
<div ng-controller="InfoCardFixture">
<bw-info-card title="title"></bw-info-card>
</div>
</body>
</html>
我关联的控制器:
/// <reference path="..\..\typings\angularjs\angular.d.ts" />
angular.module('BaseWidgetFixtures', ['bwDirectives'])
.controller('InfoCardFixture', function ($scope) {
$scope.title = "TITLE";
});
我的 Angular 指令:
/// <reference path="..\..\typings\angularjs\angular.d.ts" />
angular.module('bwDirectives', [])
.directive('bwInfoCard', function () {
return {
restrict: 'E',
transclude: false,
replace: true,
scope: { title: '=' },
template: "bw_InfoCard_Large.html",
////template: '<div>' +
//// ' Hello World! {{title}}' +
//// '</div>',
};
})
还有我的模板文件:
<div class="bw_InfoCard">
<div class="mainArea">
<div class="header">
<div class="title">Title</div>
<div class="subTitle">SubTitle</div>
</div>
<img src="" />
<div class="dataArea">
<div class="dataLabel"></div>
<div class="dataCount"></div>
</div>
</div>
<div class="stateArea">
<div class="stateLabel"></div>
</div>
<div class="actionsArea">
<div class="actionButton"></div>
<div class="actionButton"></div>
<div class="actionButton"></div>
</div>
</div>
它们都在同一个文件夹中。我正在直接从我的硬盘加载夹具 HTML 文件。 Chrome 中的网络选项卡显示了所有正在正确下载的文件,除了模板文件(根本没有显示)。
我已经为此苦苦思索了太久;它应该很简单,但可惜我在某处犯了一些错误。有人看到我的错误吗?
谢谢。
糟糕;部分原因是我打错了字。我在指令中使用 "template" 而不是 "templateUrl" 但它仍然会抛出此错误:
XMLHttpRequest cannot load file:///C:/Projects/Shared/Base.Directives/Widgets/InfoCard/bw_InfoCard_Large.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
看起来这些文件需要从网络服务器提供;没有直接从浏览器加载,但是由于没有 web.config 文件,IIS Express 不开心。
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
也许我应该使用真正的 IIS 创建一个指向该文件夹的站点?
是的,我一切正常。注意事项:
- 模板应引用为 "templateUrl:" 而不是 "template:"
- 您需要在 IIS 中托管(确保在 Windows 功能中启用您需要的其他内容,例如静态文件内容;我还启用了目录浏览以便于查找)
我正在尝试创建一组可重复使用的小部件,这些小部件可以在我创建的任何网站上使用。 AngularJS 指令似乎是解决此问题的好方法,但我在将它们与外部模板文件一起使用时遇到了问题。
如果我对指令使用内联模板,它加载得很好,但如果我使用外部模板,它会抛出错误: https://docs.angularjs.org/error/$compile/tplrt?p0=bwInfoCard&p1=
这是我的测试夹具:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Directive Test Fixture</title>
<link rel="stylesheet" href="./Style.css" type="text/css" />
<script src="../../Libraries/angular.min.js"></script>
<script src="./Widget.js"></script>
<script src="./Fixture.js"></script>
</head>
<body ng-app="BaseWidgetFixtures">
<h1>Base Info Card</h1>
<div ng-controller="InfoCardFixture">
<bw-info-card title="title"></bw-info-card>
</div>
</body>
</html>
我关联的控制器:
/// <reference path="..\..\typings\angularjs\angular.d.ts" />
angular.module('BaseWidgetFixtures', ['bwDirectives'])
.controller('InfoCardFixture', function ($scope) {
$scope.title = "TITLE";
});
我的 Angular 指令:
/// <reference path="..\..\typings\angularjs\angular.d.ts" />
angular.module('bwDirectives', [])
.directive('bwInfoCard', function () {
return {
restrict: 'E',
transclude: false,
replace: true,
scope: { title: '=' },
template: "bw_InfoCard_Large.html",
////template: '<div>' +
//// ' Hello World! {{title}}' +
//// '</div>',
};
})
还有我的模板文件:
<div class="bw_InfoCard">
<div class="mainArea">
<div class="header">
<div class="title">Title</div>
<div class="subTitle">SubTitle</div>
</div>
<img src="" />
<div class="dataArea">
<div class="dataLabel"></div>
<div class="dataCount"></div>
</div>
</div>
<div class="stateArea">
<div class="stateLabel"></div>
</div>
<div class="actionsArea">
<div class="actionButton"></div>
<div class="actionButton"></div>
<div class="actionButton"></div>
</div>
</div>
它们都在同一个文件夹中。我正在直接从我的硬盘加载夹具 HTML 文件。 Chrome 中的网络选项卡显示了所有正在正确下载的文件,除了模板文件(根本没有显示)。
我已经为此苦苦思索了太久;它应该很简单,但可惜我在某处犯了一些错误。有人看到我的错误吗?
谢谢。
糟糕;部分原因是我打错了字。我在指令中使用 "template" 而不是 "templateUrl" 但它仍然会抛出此错误:
XMLHttpRequest cannot load file:///C:/Projects/Shared/Base.Directives/Widgets/InfoCard/bw_InfoCard_Large.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
看起来这些文件需要从网络服务器提供;没有直接从浏览器加载,但是由于没有 web.config 文件,IIS Express 不开心。
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
也许我应该使用真正的 IIS 创建一个指向该文件夹的站点?
是的,我一切正常。注意事项:
- 模板应引用为 "templateUrl:" 而不是 "template:"
- 您需要在 IIS 中托管(确保在 Windows 功能中启用您需要的其他内容,例如静态文件内容;我还启用了目录浏览以便于查找)