当前位置:AngularJS API / ng / 服务(service) / $templateCache

ng

第一次使用模板,它被加载到模板缓存中,以便快速检索。你可以直接将模板标签加载到缓存中,或者通过$templateCache服务。


通过script标签:

<script type=”text/ng-template” id=”template.html”>
	<p>This is the content of the template</p>
</script>

备注:script标签模板不需要包含在文档头部。但他必须在$rootElement下,不然模板将会被忽略。

通过$templateCache服务:

var myApp = angular.module('myApp', []);
myApp.run(function($templateCache) {
  $templateCache.put('templateId.html', 'This is the content of the template');
});

检索模板后,只需使用它在您的组件:

myApp.component('myComponent', {
   templateUrl: 'templateId.html'
});

或者通过$templateCache获取它

$templateCache.get('templateId.html')