1
- import { Component , Input , Output , EventEmitter , OnInit } from '@angular/core' ;
1
+ import { Component , Input , Output , EventEmitter , OnChanges , SimpleChanges } from '@angular/core' ;
2
+ import { Subscription } from 'rxjs/Subscription' ;
2
3
3
4
import { DataSource } from '../../lib/data-source/data-source' ;
4
5
@@ -35,7 +36,7 @@ import { DataSource } from '../../lib/data-source/data-source';
35
36
</nav>
36
37
` ,
37
38
} )
38
- export class PagerComponent implements OnInit {
39
+ export class PagerComponent implements OnChanges {
39
40
40
41
@Input ( ) source : DataSource ;
41
42
@@ -46,19 +47,25 @@ export class PagerComponent implements OnInit {
46
47
protected count : number = 0 ;
47
48
protected perPage : number ;
48
49
50
+ protected dataChangedSub : Subscription ;
49
51
50
- ngOnInit ( ) {
51
- this . source . onChanged ( ) . subscribe ( ( changes ) => {
52
- this . page = this . source . getPaging ( ) . page ;
53
- this . perPage = this . source . getPaging ( ) . perPage ;
54
- this . count = this . source . count ( ) ;
55
- if ( this . isPageOutOfBounce ( ) ) {
56
- this . source . setPage ( -- this . page ) ;
52
+ ngOnChanges ( changes : SimpleChanges ) {
53
+ if ( changes . source ) {
54
+ if ( ! changes . source . firstChange ) {
55
+ this . dataChangedSub . unsubscribe ( ) ;
57
56
}
58
-
59
- this . processPageChange ( changes ) ;
60
- this . initPages ( ) ;
61
- } ) ;
57
+ this . dataChangedSub = this . source . onChanged ( ) . subscribe ( ( dataChanges ) => {
58
+ this . page = this . source . getPaging ( ) . page ;
59
+ this . perPage = this . source . getPaging ( ) . perPage ;
60
+ this . count = this . source . count ( ) ;
61
+ if ( this . isPageOutOfBounce ( ) ) {
62
+ this . source . setPage ( -- this . page ) ;
63
+ }
64
+
65
+ this . processPageChange ( dataChanges ) ;
66
+ this . initPages ( ) ;
67
+ } ) ;
68
+ }
62
69
}
63
70
64
71
/**
0 commit comments