Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何修复此角度内存泄漏?_Angularjs_Memory Leaks - Fatal编程技术网

Angularjs 如何修复此角度内存泄漏?

Angularjs 如何修复此角度内存泄漏?,angularjs,memory-leaks,Angularjs,Memory Leaks,这是我的js var app = angular.module('SearchAndResultsApp', ['angularUtils.directives.dirPagination']); app.controller('SearchAndResultsController', function($location ,$scope, $http){ $scope.fields = [ { id: 'accountNumber', name: 'Account Number', c

这是我的js

var app = angular.module('SearchAndResultsApp', ['angularUtils.directives.dirPagination']);
app.controller('SearchAndResultsController', function($location ,$scope, $http){
$scope.fields = [
    { id: 'accountNumber', name: 'Account Number', clicked: false},
    { id: 'pointOfInterestName', name: 'Point Of Interest Name', clicked: true},
    { id: 'address1', name: 'Address 1', clicked: false},
    { id: 'address2', name: 'Address 2', clicked: false},
    { id: 'city', name: 'City', clicked: false},
    { id: 'isostateCode', name: 'ISO State Code', clicked: false},
    { id: 'postalCode', name: 'Postal Code', clicked: false},
    { id: 'phone', name: 'Phone', clicked: false},
    { id: 'fax', name: 'Fax', clicked: false},
    { id: 'website', name: 'Website', clicked: false},
    { id: 'email', name: 'Email', clicked: false},
    { id: 'isocountryCode', name: 'ISO Country Code', clicked: false},
    { id: 'latitude', name: 'Latitude', clicked: false},
    { id: 'longitude', name: 'Longitude', clicked: false}
];

$scope.getSearchState = function() {
    var fields = window.location.hash.substring(1).split("&");
    if (fields.length > 0) {
        return decodeURIComponent(fields[0]);
    } else {
        return '';
    }
};

$scope.getPageState = function() {
    var fields = window.location.hash.substring(1).split("&");
    if (fields.length > 1) {
        return parseInt(fields[1]);
    } else {
        return 1;
    }
};

$scope.noResults = false;
$scope.pointsOfInterest = null;
$scope.loadingQuery = false;
$scope.orderDirection = 'asc';
$scope.glyphDirection = 'glyphicon-triangle-top';
$scope.orderedBy = 'pointOfInterestName';
$scope.previousClicked = $scope.fields[1];
$scope.lowerRange = 0;
$scope.upperRange = 0;
$scope.totalRange = 0;
$scope.currentPage = $scope.getPageState();
$scope.searchString = $scope.getSearchState();
$scope.offset = 'col-sm-offset-4';
$scope.loadingOffset = 'col-sm-offset-1';

$scope.getResultsState = function() {
  return ((($scope.pointsOfInterest == null)||($scope.pointsOfInterest[0] == null)) && ($scope.searchString != ''))
};

$scope.downloadCSV = function(selection) {
    if (selection == 'searched') {
        window.location = 'pointsOfInterest/' + encodeURIComponent($scope.searchString) + '/orderedList/' + $scope.orderedBy + '/' + $scope.orderDirection + '/csv';
    } else if (selection == 'all') {
        if (confirm("This may take some time.")){
            window.location = 'allPointsOfInterest/csv';
        }
    }
};

$scope.updateRanges = function() {
    $scope.searchString = $scope.searchString = $scope.searchString.replace(/\//g,'').replace(/\\/g,'');
    window.location.hash = encodeURIComponent($scope.searchString) + '&' + encodeURIComponent($scope.currentPage);
    if ($scope.pointsOfInterest[0] != null) {
        $scope.lowerRange = ($scope.currentPage - 1) * 20 + 1;
        $scope.totalRange = $scope.pointsOfInterest.length;
        $scope.upperRange = $scope.currentPage * 20;
    } else {
        $scope.lowerRange = 0;
        $scope.totalRange = 0;
        $scope.upperRange = 0;
    }
    if ($scope.upperRange > $scope.totalRange) {
        $scope.upperRange = $scope.totalRange;
    }
};

$scope.updateGlyphs = function(field) {
    $scope.searchString = $scope.searchString = $scope.searchString.replace(/\//g,'').replace(/\\/g,'');
    $scope.orderedBy = field.id;

    if ($scope.previousClicked != null)
        $scope.previousClicked.clicked = false;
    field.clicked = true;

    if ($scope.previousClicked == field) {
        if ($scope.orderDirection == 'asc') {
            $scope.orderDirection = 'desc';
            $scope.glyphDirection = 'glyphicon-triangle-bottom';
        } else {
            $scope.orderDirection = 'asc';
            $scope.glyphDirection = 'glyphicon-triangle-top';
        }
    } else {
         $scope.orderDirection = 'asc';
         $scope.glyphDirection = 'glyphicon-triangle-top';
    }

    $scope.updatePointsOfInterest();

    $scope.previousClicked = field;
};

$scope.updatePointsOfInterest = function() {
    if ($scope.searchString.length != 0) {
        $scope.loadingOffset = '';
        $scope.currentPage = $scope.getPageState();
        $scope.searchString = $scope.searchString.replace(/\//g,'').replace(/\\/g,'');
        window.location.hash = encodeURIComponent($scope.searchString) + '&' + encodeURIComponent($scope.currentPage);
        if ($scope.searchString == '') return;
        $scope.loadingQuery = true;
        $http.get('pointsOfInterest/' + encodeURIComponent($scope.searchString) + '/orderedList/' + $scope.orderedBy + '/' + $scope.orderDirection)
            .success(function(data) {
                $scope.pointsOfInterest = data;
                $scope.loadingQuery = false;
                $scope.loadingOffset = 'col-sm-offset-1';
                $scope.updateRanges();
                $scope.noResults = $scope.getResultsState();
                if ($scope.pointsOfInterest.length > 0) {
                    $scope.offset = '';
                } else {
                    $scope.offset = 'col-sm-offset-4';
                }
            })
            .error(function(data) {
                window.location = 'pointsOfInterest/' + encodeURIComponent($scope.searchString) + '/orderedList/' + $scope.orderedBy + '/' + $scope.orderDirection;
            });
    }
};

window.onhashchange = function() {
    $scope.updatePointsOfInterest();
}
$scope.updatePointsOfInterest();

$scope.gotoUpdatePage = function (accountNumber) {
    window.location.href = 'pointOfInterestEditor/' + accountNumber;
};

$scope.onTextClick = function ($event) {
    $event.target.select();
};
});
app.directive('ngDelay', ['$timeout', function ($timeout) {
return {
    restrict: 'A',
    scope: true,
    compile: function (element, attributes) {
        var expression = attributes['ngChange'];
        if (!expression)
            return;

        var ngModel = attributes['ngModel'];
        if (ngModel) attributes['ngModel'] = '$parent.' + ngModel;
        attributes['ngChange'] = '$$delay.execute()';

        return {
            post: function (scope, element, attributes) {
                scope.$$delay = {
                    expression: expression,
                    delay: scope.$eval(attributes['ngDelay']),
                    execute: function () {
                        var state = scope.$$delay;
                        state.then = Date.now();
                        $timeout(function () {
                            if (Date.now() - state.then >= state.delay)
                                scope.$parent.$eval(expression);
                        }, state.delay);
                    }
                };
            }
        }
    }
};
}]);
将$location添加到控制器中会导致内存泄漏。我想使用$location而不是window.location。我不确定为什么会导致内存泄漏。有人知道为什么以及如何修复它吗


编辑:它似乎发出无限的get请求。仍然不确定如何修复它。

首先,
$location
服务有一个
hash()
方法,您可以使用它来代替
window.location.hash()

第二:在执行http get请求的控制器加载上调用
$scope.updatePointsOfInterest()
方法。如果失败,它将更改url。更改url时,会重新加载控制器代码,因此,
$scope.updatePointsOfInterest()
方法会一次又一次地执行

我不确定为什么会触发
$http.error()
方法。它可能是任何东西。在开发工具(Chrome)的网络选项卡中查看您的XHR请求

如果您的
$http.error()
方法失败(这意味着存在错误,并且已正确处理),则要解决“无限循环”问题,您可以执行以下操作:
1) 或者在控制器加载时不调用
$scope.updatePointsOfInterest()

2) 或者不更改
$http.error()
方法中的url

如果您描述了使用此
updatePointsOfInterest()
方法试图实现的目标,我将能够帮助您正确编写它

问候,,
Aleksey

updatePointsOfInterest()执行一个get,该get在执行sql查询的控制器中处理,该查询返回一个感兴趣点列表。那么,通过直接在控制器中写入$scope.updatePointsOfInterest()方法,您是否正在加载初始数据?如果答案是肯定的,那么你就错了。它应该在具有resolve属性的路由器中完成。例如,看看这篇文章:如果您不想熟悉“resolves”并进行快速修复,您可以编写两个单独的方法:一个用于加载初始数据集,另一个用于更新它。在第一种情况下,不要重定向