Skip to content

Commit 79d501e

Browse files
fix(uiSref, uiState): added click unbind to prevent memory leaks
(cherry picked from commit 2df9e12)
1 parent 0cb628e commit 79d501e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/ng1/directives/stateDirectives.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ function $StateRefDirective($state: StateService, $timeout: ITimeoutService) {
158158
var type = getTypeInfo(element);
159159
var active = uiSrefActive[1] || uiSrefActive[0];
160160
var unlinkInfoFn: Function = null;
161+
var hookFn;
161162

162163
def.options = extend(defaultOpts(element, $state), attrs.uiSrefOpts ? scope.$eval(attrs.uiSrefOpts) : {});
163164

@@ -177,7 +178,11 @@ function $StateRefDirective($state: StateService, $timeout: ITimeoutService) {
177178
update();
178179

179180
if (!type.clickable) return;
180-
element.on("click", clickHook(element, $state, $timeout, type, function() { return def; }));
181+
hookFn = clickHook(element, $state, $timeout, type, function() { return def; });
182+
element.bind("click", hookFn);
183+
scope.$on('$destroy', function() {
184+
element.unbind("click", hookFn);
185+
});
181186
}
182187
};
183188
}];
@@ -211,6 +216,7 @@ function $StateRefDynamicDirective($state: StateService, $timeout: ITimeoutServi
211216
var watch = '[' + group.map(function(val) { return val || 'null'; }).join(', ') + ']';
212217
var def: Def = { state: null, params: null, options: null, href: null };
213218
var unlinkInfoFn: Function = null;
219+
var hookFn;
214220

215221
function runStateRefLink (group: any[]) {
216222
def.state = group[0]; def.params = group[1]; def.options = group[2];
@@ -225,7 +231,11 @@ function $StateRefDynamicDirective($state: StateService, $timeout: ITimeoutServi
225231
runStateRefLink(scope.$eval(watch));
226232

227233
if (!type.clickable) return;
228-
element.on("click", clickHook(element, $state, $timeout, type, function() { return def; }));
234+
hookFn = clickHook(element, $state, $timeout, type, function() { return def; });
235+
element.bind("click", hookFn);
236+
scope.$on('$destroy', function() {
237+
element.unbind("click", hookFn);
238+
});
229239
}
230240
};
231241
}];

0 commit comments

Comments
 (0)