Skip to content

Commit

Permalink
fix(uiSref, uiState): added click unbind to prevent memory leaks
Browse files Browse the repository at this point in the history
(cherry picked from commit 2df9e12)
  • Loading branch information
christopherthielen committed Aug 31, 2016
1 parent 0cb628e commit 79d501e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/ng1/directives/stateDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ function $StateRefDirective($state: StateService, $timeout: ITimeoutService) {
var type = getTypeInfo(element);
var active = uiSrefActive[1] || uiSrefActive[0];
var unlinkInfoFn: Function = null;
var hookFn;

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

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

if (!type.clickable) return;
element.on("click", clickHook(element, $state, $timeout, type, function() { return def; }));
hookFn = clickHook(element, $state, $timeout, type, function() { return def; });
element.bind("click", hookFn);
scope.$on('$destroy', function() {
element.unbind("click", hookFn);
});
}
};
}];
Expand Down Expand Up @@ -211,6 +216,7 @@ function $StateRefDynamicDirective($state: StateService, $timeout: ITimeoutServi
var watch = '[' + group.map(function(val) { return val || 'null'; }).join(', ') + ']';
var def: Def = { state: null, params: null, options: null, href: null };
var unlinkInfoFn: Function = null;
var hookFn;

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

if (!type.clickable) return;
element.on("click", clickHook(element, $state, $timeout, type, function() { return def; }));
hookFn = clickHook(element, $state, $timeout, type, function() { return def; });
element.bind("click", hookFn);
scope.$on('$destroy', function() {
element.unbind("click", hookFn);
});
}
};
}];
Expand Down

0 comments on commit 79d501e

Please sign in to comment.