angularjs - Ng-if 在 Angular js 中导致内存泄漏

标签 angularjs memory-leaks angularjs-directive angular-ng-if

Ng-if 导致 angular js 内存泄漏! 图片:https://www.mediafire.com/?ojoc55ccnyqlxyb

我的应用有两个页面

第一页是显示搜索输入的主页

第二页是释放内存的空页。

我是如何检测到泄漏的?
从链接运行我的应用程序:http://www.mediafire.com/download/y5f6f326f3zo0ch/LeakProject_-_Copy.7z
在匿名模式下使用 chrome,F12 -> 配置文件 -> 记录堆分配。 你点击主页,然后点击空白页面,重复多次,结果没有任何泄漏。

但是如果你输入任何东西来搜索,之后你会去空页面释放内存。你会漏水的。

我发现,当转到空页面时,主页中的ovNgListBox 的作用域将被破坏。 我认为 scope.textSearch 的值将更改为 undefined 并且 $scope.$watch in ngualr.js 的 ng-if 将执行以销毁范围。但反之亦然: 虽然 html 的范围

<ov-ng-list-box class="ng-isolate-scope"><div ng-init="showFilter=true" class="pull-right">

被销毁。

但范围

 <input type="text" ng-model="textSearch" ng-if="showFilter" placeholder="please type here to search..." class="search ng-scope ng-pristine ng-valid">

没有被破坏。为什么为什么?

即使您将 scope.showFilter 的值更改为 false 然后 $scope.$watch 的 in-if 没有被调用。

片段代码:

// templateleak.html <br/>

    <div class="pull-right" ng-init="showFilter=true" >
      <input type="text" class="search" placeholder="please type here to search..." ng-if="showFilter" ng-model="textSearch"/>
    </div>

// my directive
app.directive('ovNgListBox', [function () {
    return {
      restrict: 'AE',
      scope: {},
      templateUrl: 'views/template/templateLeak.html',
      controller: ['$scope',function(scope){
        console.log("Im in link of directive");
        scope.textSearch='';
        scope.search = function(){};
        scope.$on('$destroy', function(){
            scope.showFilter=false;
        });
      }]
    };
  }])


//angular.js
var ngIfDirective = ['$animate', function($animate) {
  return {
    transclude: 'element',
    priority: 600,
    terminal: true,
    restrict: 'A',
    $$tlb: true,
    link: function ($scope, $element, $attr, ctrl, $transclude) {
        var block, childScope, previousElements;
        $scope.$watch($attr.ngIf, function ngIfWatchAction(value) {

          if (toBoolean(value)) {
            if (!childScope) {
//

最佳答案

使用清除函数清除 angular.element 对象:

function dealoc(obj)
  {
  var jqCache = angular.element.cache;
  if (obj)
    {
    if (angular.isElement(obj))
      {
      cleanup(angular.element(obj));
      }
    else if (!window.jQuery)
      {
      // jQuery 2.x doesn't expose the cache storage.
      for (var key in jqCache)
        {
        var value = jqCache[key];
        if (value.data && value.data.$scope == obj)
          {
          delete jqCache[key];
          }
        }
      }
    }

  function cleanup(element)
    {
    element.off().removeData();
    if (window.jQuery)
      {
      // jQuery 2.x doesn't expose the cache storage; ensure all element data
      // is removed during its cleanup.
      jQuery.cleanData([element]);
      }
      // Note: We aren't using element.contents() here. Under jQuery,   element.contents() can fail
      // for IFRAME elements. jQuery explicitly uses (element.contentDocument ||
      // element.contentWindow.document) and both properties are null for IFRAMES that aren't attached
      // to a document.
var children = element[0].childNodes || [];
      for (var i = 0; i < children.length; i++)
        {
        cleanup(angular.element(children[i]));
        }
      }
    }

引用资料

关于angularjs - Ng-if 在 Angular js 中导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26022481/

相关文章:

c++ - SDL/OpenGL 双缓冲内存泄漏

node.js - 内存泄漏 Meteor.http

javascript - 当新项目添加到模型时,ng-repeat 更改所有项目的排序顺序

javascript - 拖放指令 - IE 中未触发事件

c++ - 我使用 vector 发生内存泄漏

无法编译带有 ng-transclude 的 AngularJs 指令模板

javascript - 从 angularjs 中的 Controller 中删除路由参数?

database - AngularJS:我需要了解数据库结构才能实现吗?

javascript - 参数 'MainController' 不是函数,未定义

angularjs - 在 ng-table 中启用分组会删除分页