Skip to content

Commit 60c9b72

Browse files
committed
Clean up before compiling a form.
Fixes memleaks and old registered inputs on form controller, fixes #200
1 parent daf6bb9 commit 60c9b72

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/directives/schema-form.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ angular.module('schemaForm')
6161
//Since we are dependant on up to three
6262
//attributes we'll do a common watch
6363
var lastDigest = {};
64-
64+
var childScope;
6565
scope.$watch(function() {
6666

6767
var schema = scope.schema;
@@ -77,8 +77,17 @@ angular.module('schemaForm')
7777
var merged = schemaForm.merge(schema, form, ignore, scope.options);
7878
var frag = document.createDocumentFragment();
7979

80+
// Create a new form and destroy the old one.
81+
// Not doing keeps old form elements hanging around after
82+
// they have been removed from the DOM
83+
// https://github.com/Textalk/angular-schema-form/issues/200
84+
if (childScope) {
85+
childScope.$destroy();
86+
}
87+
childScope = scope.$new();
88+
8089
//make the form available to decorators
81-
scope.schemaForm = {form: merged, schema: schema};
90+
childScope.schemaForm = {form: merged, schema: schema};
8291

8392
//clean all but pre existing html.
8493
element.children(':not(.schema-form-ignore)').remove();
@@ -117,7 +126,7 @@ angular.module('schemaForm')
117126
element[0].appendChild(frag);
118127

119128
//compile only children
120-
$compile(element.children())(scope);
129+
$compile(element.children())(childScope);
121130

122131
//ok, now that that is done let's set any defaults
123132
schemaForm.traverseSchema(schema, function(prop, path) {

0 commit comments

Comments
 (0)