【发布时间】:2016-05-03 14:02:10
【问题描述】:
今天我不得不修复由这段代码引起的性能问题:注意模板内部调用的updateStats
<script type="text/ng-template" id="entityGrouper">
<section>
<div>
<ul ng-click="hideEntityBox = !hideEntityBox">
<li>
{{ entityNode.name }}
</li>
<li ng-repeat="breadcrumbItem in entityNode.breadcrumb">
{{ breadcrumbItem }}
</li>
</ul>
{{ updateStats(entityNode) }}
<span ng-include="'/mypath/views/resume.html'"></span>
</div>
<div>
<div ng-repeat="entityNode in entityNode.group" ng-include="'entityGrouper'"></div>
<div ng-repeat="entity in entityNode.group" ng-include="'entityBox'"></div>
</section>
</script>
模板使用:
<div ng-repeat="entityNode in entityNode.group" ng-include="'entityGrouper'"></div>
调试这段代码后,我发现这个函数被调用的次数比数组大小要多得多(我的数组有4个对象,这个函数被调用了100多次),甚至鼠标悬停也调用了这个函数。 我通过在模板中放置一个 ng-init 解决了这个问题,现在它可以正常工作了,但我不明白为什么这个函数被调用了这么多次。双向数据绑定有什么问题吗?
【问题讨论】:
-
你是在递归调用同一个模板吗?在模板中再次调用了
<div ng-repeat="entityNode in entityNode.group" ng-include="'entityGrouper'"></div>
标签: javascript angularjs ng-template