AngularJS异常高的内存消耗



我有一个gangularjs应用程序,其呈现在200-250个元素左右。每个重复的元素都包含内部项目,其中一些元素具有嵌套的NG重复。在运行时,JS Heap内存分配约为70MB,导致该网页多次崩溃,如果没有,则一定会影响其他开放式选项卡。Batarang建议大约有3000多个示波器项目(带有$ ID(。我将指示重组到某个点,只有大约700-800个示波器项目。但是,内存消耗不在图表之外,并且性能没有明显改善。但是,在运行分析工具时,如果我单击垃圾收集器图标,则记忆消耗确实下降了约15 MB。有人可以告诉NE我应该如何调试这样一个问题吗?

更新:以下是重复的指令结构:

<li class="reminder-content">
   <wizards/>
   <editwidget></editwidget>
   <div style="font-size:87%;padding-left:5px;">{{::reminder.time| date:"shortTime"}} <span class="date-particulars">{{::reminder.time | dateFilter}}<span class="date-particulars-date">Reminder was set for <strong>{{::reminder.time | date:"longDate"}}</strong></span></span></div>
   <div class="reminder-body">
      <p class="{{reminder.trip.destination}}">{{::reminder.trip.destination}}</p>
      <p class="pad-top">{{::reminder.text}}</p>
      <p class="pad-top"  id="trav_name"><a href="{{::reminder.traveler.conversationLink}}" target="_blank">{{::reminder.traveler.name}}</a></p>
      <p>{{::reminder.wizard.id | customFilter:this}}</p>
   </div>
</li>

<reminder ng-repeat="reminder in reminders.delegatedReminders track by reminder._id| orderBy:'time'"></reminder>

ng-repeat附加观察者。您需要将所有这些优化为"单向绑定"

喜欢:

ng-repeat="item in ::$ctrl.items"

还要注意模板内部的构造,例如{{smth}}。改用{{::smth}}

另外,请勿使用$watch$broadcast$emit。始终将'<'而不是'='用作绑定。如果您使用的是Angular 1.5 ,则可以使用Life Circle钩。

$onDestroy() {
  // your code
}

这就是您可以实现它的方法:

//catch the results returned by $watch
var deReg = $scope.$watch('yourModelValue', function(){
    //your code
});
//De-register watch on destroy event
$scope.$on('$destroy', deReg);
//or you can do it following way if u have multiple watches
 $scope.$on('$destroy', function() {
   deReg();
 });

最新更新