This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +40
-12
lines changed
2 files changed +40
-12
lines changed Original file line number Diff line number Diff line change @@ -87,23 +87,31 @@ var ngIfDirective = ['$animate', function($animate) {
87
87
$$tlb : true ,
88
88
compile : function ( element , attr , transclude ) {
89
89
return function ( $scope , $element , $attr ) {
90
- var block = { } , childScope ;
90
+ var block , childScope ;
91
91
$scope . $watch ( $attr . ngIf , function ngIfWatchAction ( value ) {
92
- if ( block . startNode ) {
93
- $animate . leave ( getBlockElements ( block ) ) ;
94
- block = { } ;
95
- }
96
- if ( block . startNode ) {
97
- getBlockElements ( block ) . $destroy ( ) ;
98
- block = { } ;
99
- }
92
+
100
93
if ( toBoolean ( value ) ) {
94
+
101
95
childScope = $scope . $new ( ) ;
102
96
transclude ( childScope , function ( clone ) {
103
- block . startNode = clone [ 0 ] ;
104
- block . endNode = clone [ clone . length ++ ] = document . createComment ( ' end ngIf: ' + $attr . ngIf + ' ' ) ;
97
+ block = {
98
+ startNode : clone [ 0 ] ,
99
+ endNode : clone [ clone . length ++ ] = document . createComment ( ' end ngIf: ' + $attr . ngIf + ' ' )
100
+ } ;
105
101
$animate . enter ( clone , $element . parent ( ) , $element ) ;
106
102
} ) ;
103
+
104
+ } else {
105
+
106
+ if ( childScope ) {
107
+ childScope . $destroy ( ) ;
108
+ childScope = null ;
109
+ }
110
+
111
+ if ( block ) {
112
+ $animate . leave ( getBlockElements ( block ) ) ;
113
+ block = null ;
114
+ }
107
115
}
108
116
} ) ;
109
117
} ;
Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ describe('ngIf', function () {
36
36
expect ( element . children ( ) . length ) . toBe ( 0 ) ;
37
37
} ) ;
38
38
39
- it ( 'should create a new scope' , function ( ) {
39
+ it ( 'should create a new scope every time the expression evaluates to true ' , function ( ) {
40
40
$scope . $apply ( 'value = true' ) ;
41
41
element . append ( $compile (
42
42
'<div ng-if="value"><span ng-init="value=false"></span></div>'
@@ -45,6 +45,26 @@ describe('ngIf', function () {
45
45
expect ( element . children ( 'div' ) . length ) . toBe ( 1 ) ;
46
46
} ) ;
47
47
48
+ it ( 'should destroy the child scope every time the expression evaluates to false' , function ( ) {
49
+ $scope . value = true ;
50
+ element . append ( $compile (
51
+ '<div ng-if="value"></div>'
52
+ ) ( $scope ) ) ;
53
+ $scope . $apply ( ) ;
54
+
55
+ var childScope = element . children ( ) . scope ( ) ;
56
+ var destroyed = false ;
57
+
58
+ childScope . $on ( '$destroy' , function ( ) {
59
+ destroyed = true ;
60
+ } ) ;
61
+
62
+ $scope . value = false ;
63
+ $scope . $apply ( ) ;
64
+
65
+ expect ( destroyed ) . toBe ( true ) ;
66
+ } ) ;
67
+
48
68
it ( 'should play nice with other elements beside it' , function ( ) {
49
69
$scope . values = [ 1 , 2 , 3 , 4 ] ;
50
70
element . append ( $compile (
You can’t perform that action at this time.
0 commit comments