@@ -17,6 +17,7 @@ import {
17
17
ElementRef ,
18
18
ChangeDetectionStrategy ,
19
19
ViewEncapsulation ,
20
+ ChangeDetectorRef ,
20
21
} from '@angular/core' ;
21
22
import {
22
23
trigger ,
@@ -63,24 +64,25 @@ export const HIDE_ANIMATION = '195ms cubic-bezier(0.0,0.0,0.2,1)';
63
64
} ,
64
65
animations : [
65
66
trigger ( 'state' , [
66
- state ( 'void' , style ( { transform : 'translateY(100%)' } ) ) ,
67
- state ( 'initial' , style ( { transform : 'translateY(100%)' } ) ) ,
67
+ state ( 'void, initial, complete' , style ( { transform : 'translateY(100%)' } ) ) ,
68
68
state ( 'visible' , style ( { transform : 'translateY(0%)' } ) ) ,
69
- state ( 'complete' , style ( { transform : 'translateY(100%)' } ) ) ,
70
69
transition ( 'visible => complete' , animate ( HIDE_ANIMATION ) ) ,
71
70
transition ( 'initial => visible, void => visible' , animate ( SHOW_ANIMATION ) ) ,
72
71
] )
73
72
] ,
74
73
} )
75
74
export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
75
+ /** Whether the component has been destroyed. */
76
+ private _destroyed = false ;
77
+
76
78
/** The portal host inside of this container into which the snack bar content will be loaded. */
77
79
@ViewChild ( PortalHostDirective ) _portalHost : PortalHostDirective ;
78
80
79
81
/** Subject for notifying that the snack bar has exited from view. */
80
- private onExit : Subject < any > = new Subject ( ) ;
82
+ _onExit : Subject < any > = new Subject ( ) ;
81
83
82
84
/** Subject for notifying that the snack bar has finished entering the view. */
83
- private onEnter : Subject < any > = new Subject ( ) ;
85
+ _onEnter : Subject < any > = new Subject ( ) ;
84
86
85
87
/** The state of the snack bar animations. */
86
88
animationState : SnackBarState = 'initial' ;
@@ -91,7 +93,8 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
91
93
constructor (
92
94
private _ngZone : NgZone ,
93
95
private _renderer : Renderer2 ,
94
- private _elementRef : ElementRef ) {
96
+ private _elementRef : ElementRef ,
97
+ private _changeDetectorRef : ChangeDetectorRef ) {
95
98
super ( ) ;
96
99
}
97
100
@@ -126,7 +129,7 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
126
129
if ( event . toState === 'visible' ) {
127
130
// Note: we shouldn't use `this` inside the zone callback,
128
131
// because it can cause a memory leak.
129
- const onEnter = this . onEnter ;
132
+ const onEnter = this . _onEnter ;
130
133
131
134
this . _ngZone . run ( ( ) => {
132
135
onEnter . next ( ) ;
@@ -137,30 +140,21 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
137
140
138
141
/** Begin animation of snack bar entrance into view. */
139
142
enter ( ) : void {
140
- this . animationState = 'visible' ;
141
- }
142
-
143
- /** Returns an observable resolving when the enter animation completes. */
144
- _onEnter ( ) : Observable < void > {
145
- this . animationState = 'visible' ;
146
- return this . onEnter . asObservable ( ) ;
143
+ if ( ! this . _destroyed ) {
144
+ this . animationState = 'visible' ;
145
+ this . _changeDetectorRef . detectChanges ( ) ;
146
+ }
147
147
}
148
148
149
149
/** Begin animation of the snack bar exiting from view. */
150
150
exit ( ) : Observable < void > {
151
151
this . animationState = 'complete' ;
152
- return this . _onExit ( ) ;
152
+ return this . _onExit ;
153
153
}
154
154
155
- /** Returns an observable that completes after the closing animation is done. */
156
- _onExit ( ) : Observable < void > {
157
- return this . onExit . asObservable ( ) ;
158
- }
159
-
160
- /**
161
- * Makes sure the exit callbacks have been invoked when the element is destroyed.
162
- */
155
+ /** Makes sure the exit callbacks have been invoked when the element is destroyed. */
163
156
ngOnDestroy ( ) {
157
+ this . _destroyed = true ;
164
158
this . _completeExit ( ) ;
165
159
}
166
160
@@ -171,7 +165,7 @@ export class MdSnackBarContainer extends BasePortalHost implements OnDestroy {
171
165
private _completeExit ( ) {
172
166
// Note: we shouldn't use `this` inside the zone callback,
173
167
// because it can cause a memory leak.
174
- const onExit = this . onExit ;
168
+ const onExit = this . _onExit ;
175
169
176
170
first . call ( this . _ngZone . onMicrotaskEmpty ) . subscribe ( ( ) => {
177
171
onExit . next ( ) ;
0 commit comments