aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgor Minar2012-03-14 13:03:53 -0700
committerIgor Minar2012-03-14 14:33:20 -0700
commit2315d9b3610994b36c44e4a97fb1427d59471ce8 (patch)
treeb8ac9c4204da71ab29e21eebe63af20f0ca6eccb
parent8fd1b748721a6312c9d4340744a012143e4d5ef2 (diff)
downloadangular.js-2315d9b3610994b36c44e4a97fb1427d59471ce8.tar.bz2
fix(ng-switch): properly destroy child scopes
-rw-r--r--src/directive/ngSwitch.js10
-rw-r--r--test/directive/ngSwitchSpec.js30
2 files changed, 36 insertions, 4 deletions
diff --git a/src/directive/ngSwitch.js b/src/directive/ngSwitch.js
index 59c7056e..16b0c4d4 100644
--- a/src/directive/ngSwitch.js
+++ b/src/directive/ngSwitch.js
@@ -69,19 +69,21 @@ var ngSwitchDirective = valueFn({
element.data(NG_SWITCH, cases);
return function(scope, element){
var selectedTransclude,
- selectedElement;
+ selectedElement,
+ selectedScope;
scope.$watch(watchExpr, function(value) {
if (selectedElement) {
+ selectedScope.$destroy();
selectedElement.remove();
- selectedElement = null;
+ selectedElement = selectedScope = null;
}
if ((selectedTransclude = cases['!' + value] || cases['?'])) {
scope.$eval(attr.change);
- selectedTransclude(scope.$new(), function(caseElement, scope) {
+ selectedScope = scope.$new();
+ selectedTransclude(selectedScope, function(caseElement) {
selectedElement = caseElement;
element.append(caseElement);
- element.bind('$destroy', bind(scope, scope.$destroy));
});
}
});
diff --git a/test/directive/ngSwitchSpec.js b/test/directive/ngSwitchSpec.js
index c61dd60d..b0dd7493 100644
--- a/test/directive/ngSwitchSpec.js
+++ b/test/directive/ngSwitchSpec.js
@@ -60,4 +60,34 @@ describe('ng-switch', function() {
expect($rootScope.name).toEqual('works');
expect(element.text()).toEqual('works');
}));
+
+
+ it('should properly create and destory child scopes', inject(function($rootScope, $compile) {
+ element = $compile(
+ '<ng:switch on="url">' +
+ '<div ng-switch-when="a">{{name}}</div>' +
+ '</ng:switch>')($rootScope);
+ $rootScope.$apply();
+
+ var getChildScope = function() { return element.find('div').scope(); };
+
+ expect(getChildScope()).toBeUndefined();
+
+ $rootScope.url = 'a';
+ $rootScope.$apply();
+ var child1 = getChildScope();
+ expect(child1).toBeDefined();
+ spyOn(child1, '$destroy');
+
+ $rootScope.url = 'x';
+ $rootScope.$apply();
+ expect(getChildScope()).toBeUndefined();
+ expect(child1.$destroy).toHaveBeenCalledOnce();
+
+ $rootScope.url = 'a';
+ $rootScope.$apply();
+ var child2 = getChildScope();
+ expect(child2).toBeDefined();
+ expect(child2).not.toBe(child1);
+ }));
});