Skip to content

Commit 2df9e12

Browse files
author
Sergey Melyukov
committed
fix(uiSref, uiState): added click unbind to prevent memory leaks
1 parent 14ec606 commit 2df9e12

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/stateDirectives.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ function $StateRefDirective($state, $timeout) {
124124
var type = getTypeInfo(element);
125125
var active = uiSrefActive[1] || uiSrefActive[0];
126126
var unlinkInfoFn = null;
127+
var hookFn;
127128

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

@@ -143,7 +144,11 @@ function $StateRefDirective($state, $timeout) {
143144
update();
144145

145146
if (!type.clickable) return;
146-
element.bind("click", clickHook(element, $state, $timeout, type, function() { return def; }));
147+
hookFn = clickHook(element, $state, $timeout, type, function() { return def; });
148+
element.bind("click", hookFn);
149+
scope.$on('$destroy', function() {
150+
element.unbind("click", hookFn);
151+
});
147152
}
148153
};
149154
}
@@ -176,6 +181,7 @@ function $StateRefDynamicDirective($state, $timeout) {
176181
var watch = '[' + group.map(function(val) { return val || 'null'; }).join(', ') + ']';
177182
var def = { state: null, params: null, options: null, href: null };
178183
var unlinkInfoFn = null;
184+
var hookFn;
179185

180186
function runStateRefLink (group) {
181187
def.state = group[0]; def.params = group[1]; def.options = group[2];
@@ -190,7 +196,11 @@ function $StateRefDynamicDirective($state, $timeout) {
190196
runStateRefLink(scope.$eval(watch));
191197

192198
if (!type.clickable) return;
193-
element.bind("click", clickHook(element, $state, $timeout, type, function() { return def; }));
199+
hookFn = clickHook(element, $state, $timeout, type, function() { return def; });
200+
element.bind("click", hookFn);
201+
scope.$on('$destroy', function() {
202+
element.unbind("click", hookFn);
203+
});
194204
}
195205
};
196206
}

0 commit comments

Comments
 (0)