你可以让你的模板使用 Angular 服务注册它们的链接,然后在你的 head 标签中添加一个控制器来填充链接列表。
这是一个例子: http: //plnkr.co/edit/1a6U9f
app.factory('LinkService', function() {
var links = {};
return {
links: links,
addLink: function(href, relation) {
links[href] = { href: href, relation: relation };
},
removeLink: function(href) {
delete links[href];
}
};
});
app.controller('LinkController', function($scope, LinkService) {
$scope.links = LinkService.links;
});
app.controller('Template1Controller', function($scope, LinkService) {
LinkService.addLink('template1.css','stylesheet');
$scope.$on('$destroy', function() {
LinkService.removeLink('template1.css');
});
});
然后在您的 html 中(用于演示目的的内联部分):
<head ng-controller="LinkController">
...
<link rel="{{link.relation}}" ng-href="{{link.href}}" ng-repeat="link in links">
</head>
<body ng-controller="MainCtrl">
<!-- You could use ng-view here -->
<ng-include src="someTemplateField"></ng-include>
<script type="text/ng-template" id="template1">
<div class="red" ng-controller="Template1Controller">Template 1</div>
</script>
</body>