Skip to content

Instantly share code, notes, and snippets.

Created August 8, 2013 06:33
Show Gist options
  • Save anonymous/6181994 to your computer and use it in GitHub Desktop.
Save anonymous/6181994 to your computer and use it in GitHub Desktop.
Memory leaks that occurs in IE9 when using ng-view. It seems that when i clean the memory before navigating and after navigating, this memory issue will not occur.
h1
<div>
<button ng-click="setItems()">Click Me</button>
<button ng-click="clearItems()">clear Me</button>
<a href="#/v2">v2</a>
<table>
<tr ng-repeat="a in arr">
<td ng-bind-html-unsafe="a['name0']">
</td>
</tr>
</table>
<ng-switch on="filter.test">
<div ng-switch-when="SD">
</div>
</ng-switch>
</div>
h2
<div>
<button ng-click="setItems()">Click Me</button>
<button ng-click="clearItems()">clear Me</button>
<a href="#/v1">v1</a>
<table>
<tr ng-repeat="a in arr">
<td ng-bind-html-unsafe="a['name0']">
</td>
</tr>
</table>
<ng-switch on="filter.test">
<div ng-switch-when="SD">
</div>
</ng-switch>
</div>
<!doctype html>
<html ng-app="test">
<head>
<script src="../angular.min.js"></script>
</head>
<body>
<div ng-view>
</div>
<script>
function log(s) {
if (window.console) console.log(s);
}
var test = angular.module("test", []).config(['$routeProvider', function ($routeProvider) {
$routeProvider.when("/v1", {controller:'MyControler', templateUrl:"h1.html"});
$routeProvider.when("/v2", { controller: 'MyControler', templateUrl: "h2.html" });
$routeProvider.otherwise({ redirectTo: '/v1' });
}]);
function MyControler($scope, $templateCache) {
/*
This function prevents the leaks. Without it, the IE9 will never free all the resources.
There are two types of leaks it addresses: one results from the ng-switch-when and the
other results from the ng-repeat.<br>
The leak occurs only when ng-view and routing are involved.
*/
(function PREVENT_LEAKS() {
// this handler handles the ng-repeat problem
$scope.$on("$routeChangeStart", function () {
$templateCache.removeAll();
$scope.clearItems();
log("Starting Moving Out of scope: " + $scope.$id);
});
// this handler handles the switch problem
$scope.$on("$routeChangeSuccess", function () {
log("Moving into of scope: " + $scope.$id);
$scope.filter =undefined;
});
})();
$scope.arr = [];
$scope.filter = {
test:"SD"
};
$scope.setItems = function () {
for (var i = 0; i < 600; i++) {
$scope.arr[i] = {};
for (var j = 0; j < 600; j++) {
$scope.arr[i]["name" + j] = "<a href='http://google.com'>test</a>";
}
}
}
$scope.clearItems = function () {
for (var i = 0; i < $scope.arr.length; i++) {
delete $scope.arr[i];
}
$scope.arr = [];
}
$scope.setItems();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment