我有一个ng-repeat,它遍历一个对象数组。在每个循环中,值'模板'和& “值”已初始化。以下版本工作并使用ng-include加载模板,但它恰好非常慢:
<tr class="tableRowsDocs" ng-repeat="dbo in rows track by $index">
<div ng-init="values = dbo.get4(attobj.key); key = attobj.key; template = attobj.template || getAttributeTemplate(dbo.clazz + attobj.key);">
<div class="content" ng-include="template"></div>
</div>
</td>
</tr>
模板和值的值是动态的,但它始终保存模板/脚本的ID,如:
<script type="text/ng-template" id="links_as_dns_template">
<div ng-repeat="dbo in values track by $index" ng-include="'link_as_dn_template'"></div>
</script>
<script type="text/ng-template" id="link_as_dn_template">
<a href="#/view/{{ dbo.cid }}"><p>{{ dbo.displayName() }}</p></a>
</script
请注意,被调用的模板也使用ng-include来调用第二个模板。
我试图通过使用自定义指令加载模板来加快速度,但似乎无法使以下示例在我的情况下工作:
app.directive('ngInline', [
'$templateCache',
function($templateCache) {
return {
restrict: 'A',
priority: 400, // Same as ng-include.
compile: function(element, attrs){
var templateName = attrs.ngInline;
if(!templateName){
throw new Error('ngInline: expected template name');
}
var template = $templateCache.get(templateName);
if(angular.isUndefined(template)){
throw new Error('ngInline: unknown template ' + templateName);
}
element.html(template);
}
};
}
]);
任何人都可以向我解释如何正确有效地执行此操作(平均100行x35列 - >多值单元格/渲染)
这个例子来自: http://zachsnow.com/#!/blog/2014/angularjs-faster-ng-include/
答案 0 :(得分:0)
指令:
app.directive('customtemp', function($templateCache, $compile) {
return {
link: function(scope, element, attrs) {
var z = $templateCache.get(scope.template);
element.html(z);
$compile(element.contents())(scope);
}
}
});
主要模板:
<tr class="tableRowsDocs" ng-repeat="dbo in rows track by $index">
<td class="tableColumnsDocs" ng-repeat="attobj in columns track by $index">
<customtemp ng-init="values = dbo.get4(attobj.key); key = attobj.key; template = attobj.template || getAttributeTemplate(dbo.clazz + attobj.key);">
</customtemp>
</td>
</tr>
示例已加载/调用的模板:
<script type="text/ng-template" id="links_as_dns_template">
<div ng-repeat="dbo in values track by $index" ng-include="'link_as_dn_template'"></div>
</script>
<script type="text/ng-template" id="link_as_dn_template">
<a href="#/view/{{ dbo.cid }}"><p>{{ dbo.displayName() }}</p></a>
</script>
结果比使用ng-include快4倍。