简体   繁体   中英

Infinite polling with Angular 4 and Http observables

I'm trying to build an infinite polling in my Http service because I'm building a king of of dashborad who survey data's comming from a servor, here's my code who's almost working (in my console I see the Json comming but it doesn't reflect to my view...I would like to inject in my users: Observable

const usersURL = 'http://my.super.servor.php'

@Injectable()
export class UserService {

 users: Observable<User[]>

   constructor (public http:Http) {
     this.users = http.get(usersURL)
              genre mobile ou autre
              .map(res => [res.json()]);

        let i = this.users.subscribe(
          usersURL => console.log(usersURL),
         () => {}, // Here we catch up errors
         () => console.log("completed!") // Here we catch up if its completed
        )

    // Here's where I'm trying to do the polling every 5 secondes
    let tick$ = Observable.interval(5000);

    let response$ = 
      tick$
          .flatMap(() => http.get(usersURL))
          .map(res => [res.json()]);

    let stockPoller = response$.subscribe(res => console.log(res));
  }

您只需将您的polling observable分配给this.users

this.users = tick$.flatMap(() => http.get(usersURL)).map(res => [res.json()]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM