不确定我是否弄错了。我无法通过脚本标记从文件中包含模板。有什么想法吗?
模板:
<script type="text/ng-template" id="test1">inside</script>
<script type="text/ng-template" id="test2" src="templateFile.html"></script>
<div ng-controller="MyCtrl">
Select:
<a href ng:click="tpl='first.html'">internal</a>
| <a href ng:click="tpl='test1'">script inside</a>
| <a href ng:click="tpl='test2'">script external</a>
<div style="border: 1px solid;min-height: 20px">
<ng:include src="tpl"></ng:include>
</div>
</div>
控制器:
var myApp = angular.module('myApp', []);
function MyCtrl($scope, $templateCache) {
$templateCache.put('first.html', 'First template');
}
JSFiddle:http://jsfiddle.net/aG8Zy/32/
答案 0 :(得分:1)
实际上stackoverflow上存在所有必需的信息
我试着总结一下:
模板:
<script type="text/ng-template" id="test1" src="https://tools.ietf.org/html/rfc1918">inside</script>
<script type="text/ng-template" id="test2" src="http://mhnystatic.s3.amazonaws.com/angulartest/list.html"></script>
<div ng-controller="MyCtrl">
Select:
<a href ng:click="tpl='first.html'">internal</a>
| <a href ng:click="openTemplate('test1')">ietf.org</a>
| <a href ng:click="openTemplate('test2')">amazonaws</a>
<div style="border: 1px solid;min-height: 20px"><ng:include src="tpl"></ng:include></div>
</div>
应用:
var myApp = angular.module('myApp', [])
.config(function($sceDelegateProvider) {
$sceDelegateProvider.resourceUrlWhitelist([
// Allow same origin resource loads.
'self',
// Allow loading from outer templates domain.
'https://tools.ietf.org/**',
'http://mhnystatic.s3.amazonaws.com/**'
]);
})
.controller('MyCtrl', function($scope, $templateCache, $sce) {
$templateCache.put('first.html', 'First template');
$scope.openTemplate = function(id) {
var src = document.querySelector('script[id="' + id + '"]').getAttribute('src');
$scope.tpl = $sce.getTrustedResourceUrl(src);
};
});
jsFiddle:http://jsfiddle.net/glebv/msfbr1xr/15/
但正如您所见,域amazonaws.com可用于加载资源,但tools.ietf.org不允许。 它们都被添加到WhiteList
您应该使用允许CORS的方法。