1
1
import {
2
+ AfterContentInit ,
2
3
AfterViewInit ,
3
4
Component ,
4
5
ContentChildren ,
5
6
ElementRef ,
6
7
EventEmitter ,
7
8
HostBinding ,
8
9
Input ,
9
- NgZone ,
10
10
OnDestroy ,
11
11
Output ,
12
12
QueryList ,
13
13
Renderer2 ,
14
14
ViewChild
15
15
} from '@angular/core' ;
16
16
import { Subscription } from 'rxjs/Subscription' ;
17
- import { first } from 'rxjs/operators/first' ;
18
-
19
17
import { toBoolean } from '../core/util/convert' ;
20
18
21
19
import { NzCarouselContentDirective } from './nz-carousel-content.directive' ;
@@ -67,15 +65,15 @@ import { NzCarouselContentDirective } from './nz-carousel-content.directive';
67
65
`
68
66
]
69
67
} )
70
- export class NzCarouselComponent implements AfterViewInit , OnDestroy {
68
+ export class NzCarouselComponent implements AfterViewInit , OnDestroy , AfterContentInit {
71
69
private _autoPlay = false ;
72
70
private _dots = true ;
73
71
private _vertical = false ;
74
72
private _effect = 'scrollx' ;
75
73
slideContentsSubscription : Subscription ;
76
74
activeIndex = 0 ;
77
75
transform = 'translate3d(0px, 0px, 0px)' ;
78
- interval ;
76
+ timeout ;
79
77
80
78
@ContentChildren ( NzCarouselContentDirective ) slideContents : QueryList < NzCarouselContentDirective > ;
81
79
@ViewChild ( 'slickList' ) slickList : ElementRef ;
@@ -153,7 +151,6 @@ export class NzCarouselComponent implements AfterViewInit, OnDestroy {
153
151
154
152
renderContent ( ) : void {
155
153
if ( this . slideContents && this . slideContents . length ) {
156
- this . slideContents . first . isActive = true ;
157
154
this . slideContents . forEach ( ( content , i ) => {
158
155
content . width = this . elementRef . nativeElement . offsetWidth ;
159
156
if ( this . nzEffect === 'fade' ) {
@@ -187,9 +184,9 @@ export class NzCarouselComponent implements AfterViewInit, OnDestroy {
187
184
}
188
185
189
186
setUpAutoPlay ( ) : void {
190
- this . clearInterval ( ) ;
187
+ this . clearTimeout ( ) ;
191
188
if ( this . nzAutoPlay ) {
192
- this . interval = setInterval ( _ => {
189
+ this . timeout = setTimeout ( _ => {
193
190
this . setActive ( this . slideContents . toArray ( ) [ this . nextIndex ] , this . nextIndex ) ;
194
191
} , 3000 ) ;
195
192
}
@@ -202,10 +199,10 @@ export class NzCarouselComponent implements AfterViewInit, OnDestroy {
202
199
}
203
200
}
204
201
205
- clearInterval ( ) : void {
206
- if ( this . interval ) {
207
- clearInterval ( this . interval ) ;
208
- this . interval = null ;
202
+ clearTimeout ( ) : void {
203
+ if ( this . timeout ) {
204
+ clearTimeout ( this . timeout ) ;
205
+ this . timeout = null ;
209
206
}
210
207
}
211
208
@@ -233,24 +230,28 @@ export class NzCarouselComponent implements AfterViewInit, OnDestroy {
233
230
}
234
231
}
235
232
236
- constructor ( public elementRef : ElementRef , private renderer : Renderer2 , private zone : NgZone ) {
233
+ constructor ( public elementRef : ElementRef , private renderer : Renderer2 ) {
234
+ }
235
+
236
+ ngAfterContentInit ( ) : void {
237
+ if ( this . slideContents && this . slideContents . length ) {
238
+ this . slideContents . first . isActive = true ;
239
+ }
237
240
}
238
241
239
242
ngAfterViewInit ( ) : void {
240
243
this . slideContentsSubscription = this . slideContents . changes . subscribe ( ( ) => {
241
244
this . renderContent ( ) ;
242
245
} ) ;
243
- this . zone . onStable . pipe ( first ( ) ) . subscribe ( ( ) => {
244
- this . renderContent ( ) ;
245
- } ) ;
246
+ this . renderContent ( ) ;
246
247
}
247
248
248
249
ngOnDestroy ( ) : void {
249
250
if ( this . slideContentsSubscription ) {
250
251
this . slideContentsSubscription . unsubscribe ( ) ;
251
252
this . slideContentsSubscription = null ;
252
253
}
253
- this . clearInterval ( ) ;
254
+ this . clearTimeout ( ) ;
254
255
}
255
256
256
257
}
0 commit comments