I assume that due to misuse, the following code snippet creates a memory leak, I don’t understand why, I wrote a short screencast to demonstrate the problem, you can find it here: https://www.youtube.com/watch?v = IWCcOI5kN1c & feature = youtu.be
Here is the error code: it just dynamically compiles the html stored in the variable called content, but as soon as you start changing the variable contentthrough the text field, the code starts to create $ watch'es the situation gets worse as soon as you increase the number of bindings, since every Angular binding creates a new scope (which is obviously correct), but does not delete the old ones, and they are stored in memory
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.15/angular.js" data-semver="1.3.15"></script>
<script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
app.directive('compile', function($compile) {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
scope.$watch(attrs.compile, function(newVal) {
var newElem = document.createElement('span');
newElem.innerHTML = newVal;
elem[0].innerHTML = '';
elem[0].appendChild(newElem);
try {
$compile(newElem)(scope);
} catch(e) {
}
});
}
};
});
</script>
</head>
<body ng-controller="MainCtrl" ng-init="variable = 3; content = '{{ variable }}'">
<div>
The value of $scope.variable === "{{ variable }}"
</div>
<div>
The value of $scope.content === "{{ content }}"
</div>
<br>
<div>
The value of $scope.content is <b>ng-model</b>'ed via the following textarea:<br>
</div>
<textarea rows="3" ng-model="content"></textarea>
<div style="border: 1px solid black">
Instead of rendering the value of the $scope.content field which is currently equal to "{{ content }}" I need to render compiled and evaluated value which should be equal to "{{ variable }}"
</div>
<hr>
<p>Look! It works: <span compile="content"></span></p>
</body>
</html>
source
share