This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +36
-4
lines changed
2 files changed +36
-4
lines changed Original file line number Diff line number Diff line change @@ -69,19 +69,21 @@ var ngSwitchDirective = valueFn({
69
69
element . data ( NG_SWITCH , cases ) ;
70
70
return function ( scope , element ) {
71
71
var selectedTransclude ,
72
- selectedElement ;
72
+ selectedElement ,
73
+ selectedScope ;
73
74
74
75
scope . $watch ( watchExpr , function ( value ) {
75
76
if ( selectedElement ) {
77
+ selectedScope . $destroy ( ) ;
76
78
selectedElement . remove ( ) ;
77
- selectedElement = null ;
79
+ selectedElement = selectedScope = null ;
78
80
}
79
81
if ( ( selectedTransclude = cases [ '!' + value ] || cases [ '?' ] ) ) {
80
82
scope . $eval ( attr . change ) ;
81
- selectedTransclude ( scope . $new ( ) , function ( caseElement , scope ) {
83
+ selectedScope = scope . $new ( ) ;
84
+ selectedTransclude ( selectedScope , function ( caseElement ) {
82
85
selectedElement = caseElement ;
83
86
element . append ( caseElement ) ;
84
- element . bind ( '$destroy' , bind ( scope , scope . $destroy ) ) ;
85
87
} ) ;
86
88
}
87
89
} ) ;
Original file line number Diff line number Diff line change @@ -60,4 +60,34 @@ describe('ng-switch', function() {
60
60
expect ( $rootScope . name ) . toEqual ( 'works' ) ;
61
61
expect ( element . text ( ) ) . toEqual ( 'works' ) ;
62
62
} ) ) ;
63
+
64
+
65
+ it ( 'should properly create and destory child scopes' , inject ( function ( $rootScope , $compile ) {
66
+ element = $compile (
67
+ '<ng:switch on="url">' +
68
+ '<div ng-switch-when="a">{{name}}</div>' +
69
+ '</ng:switch>' ) ( $rootScope ) ;
70
+ $rootScope . $apply ( ) ;
71
+
72
+ var getChildScope = function ( ) { return element . find ( 'div' ) . scope ( ) ; } ;
73
+
74
+ expect ( getChildScope ( ) ) . toBeUndefined ( ) ;
75
+
76
+ $rootScope . url = 'a' ;
77
+ $rootScope . $apply ( ) ;
78
+ var child1 = getChildScope ( ) ;
79
+ expect ( child1 ) . toBeDefined ( ) ;
80
+ spyOn ( child1 , '$destroy' ) ;
81
+
82
+ $rootScope . url = 'x' ;
83
+ $rootScope . $apply ( ) ;
84
+ expect ( getChildScope ( ) ) . toBeUndefined ( ) ;
85
+ expect ( child1 . $destroy ) . toHaveBeenCalledOnce ( ) ;
86
+
87
+ $rootScope . url = 'a' ;
88
+ $rootScope . $apply ( ) ;
89
+ var child2 = getChildScope ( ) ;
90
+ expect ( child2 ) . toBeDefined ( ) ;
91
+ expect ( child2 ) . not . toBe ( child1 ) ;
92
+ } ) ) ;
63
93
} ) ;
You can’t perform that action at this time.
0 commit comments