import { AppGlobal , AppService } from './../../app/app.service' ; import { Component , ViewChild } from '@angular/core' ; import { NavController , IonicPage } from 'ionic-angular' ; @ IonicPage ( ) @ Component ( { selector : 'page-contact' , templateUrl : 'contact.html' } ) export class ContactPage { @ ViewChild ( 'scroll' ) scrollElement : any ; @ ViewChild ( 'spinner' ) spinnerElement : any ; categories : Array < any > = [ ] ; selectedMenuTarget : any ; products : Array < any > = [ ] ; hasmore = true ; islock = false ; params = { favoritesId : 0 , pageNo : 1 } ; constructor ( public navCtrl , public appService ) { } ionViewDidLoad ( ) { this . getCategories ( ) ; this . addScrollEventListener ( ) ; } addScrollEventListener ( ) { this . scrollElement . _scrollContent . nativeElement . onscroll = event => { if ( this . spinnerElement ) { var top = this . spinnerElement . nativeElement . getBoundingClientRect ( ) . top ; var clientHeight = document . documentElement . clientHeight ; if ( top <= clientHeight ) { console . log ( "ready loadmore..." ) ; this . doInfinite ( ) ; } } } ; } getCategories ( ) { this . appService . httpGet ( AppGlobal . API . getCategories , { appTag : 'dress' } , rs => { console . debug ( rs ) ; this . categories = rs . data ; this . params . favoritesId = this . categories [ 0 ] . FavoritesId ; this . getProducts ( ) ; } ) ; } itemClick ( c , event ) { var initSelected = document . getElementsByClassName ( 'menuItem' ) ; if ( initSelected [ 0 ] . classList . contains ( "active" ) ) { initSelected [ 0 ] . classList . remove ( "active" ) ; } if ( this . selectedMenuTarget ) { this . selectedMenuTarget . classList . remove ( "active" ) ; } event . currentTarget . classList . add ( "active" ) ; this . selectedMenuTarget = event . currentTarget ; this . hasmore = true ; this . params . favoritesId = c . FavoritesId ; this . params . pageNo = 1 ; this . products = [ ] ; this . getProducts ( ) ; } getProducts ( ) { this . appService . httpGet ( AppGlobal . API . getProducts , this . params , rs => { this . products = rs . data ; this . params . pageNo += 1 ; } ) ; } doInfinite ( ) { if ( this . islock ) { return ; } if ( this . hasmore == false ) { return ; } this . islock = true ; this . appService . httpGet ( AppGlobal . API . getProducts , this . params , d => { this . islock = false ; if ( d . data . length > 0 ) { this . products = this . products . concat ( d . data ) ; this . params . pageNo += 1 ; } else { this . hasmore = false ; console . log ( "没有数据啦！" ) ; } } ) ; } goDetails ( item ) { this . navCtrl . push ( 'ProductDetailsPage' , { item : item } ) ; } }