我正在开发AngularJS。之前,我使用ng-include和URL一起使用。但是如何将URL指向TemplateCache呢?
<ng-include
src="string"
[onload="string"]
[autoscroll="string"]>
...
</ng-include>
我正在开发AngularJS。之前,我使用ng-include和URL一起使用。但是如何将URL指向TemplateCache呢?
<ng-include
src="string"
[onload="string"]
[autoscroll="string"]>
...
</ng-include>
模板缓存使用一个键来标识缓存元素,因此你可以使用该键。
$templateCache.put('MY KEY', 'Cached content');
并且在HTML中:
<ng-include src="'MY KEY'"></ng-include>
请参见AngularJS文档的$templateCache部分。
有两种方法:
在标记中:
将您的模板指定为脚本标签:
<script type="text/ng-template" id="templateId.html">
<p>This is the content of the template</p>
</script>
(这应该是您的ng-app
的后代,换句话说,它应该在ng-app
标记内的某个地方指定)
这将自动缓存模板。
代码示例:
var myApp = angular.module('myApp', []);
myApp.run(function($templateCache) {
$templateCache.put('templateId.html', 'This is the content of the template');
});
<div ng-include=" 'templateId.html' "></div>
或者
$templateCache.get('templateId.html')
$http
服务来获取模板,然后将它们存储在$templateCache
中。 - Ron Dadon