Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Routing docs should use idiomatic Observable syntax #2675

Closed
@robwormald

Description

@robwormald

Ref: https://angular.io/docs/ts/latest/tutorial/toh-pt5.html

current example:

ngOnInit(): void {
  this.route.params.forEach((params: Params) => {
    let id = +params['id'];
    this.heroService.getHero(id)
      .then(hero => this.hero = hero);
  });
}

This is rather hard to parse (and not exactly side effect free)

Better syntax would be

this.routeParams.flatMap(params => this.heroService.getHero(params.id))

or

this.routeParams.switchMap(params => this.heroService.getHero(params.id))

which would cancel in flight requests - the current syntax has the possibility of an inflight request arriving after the user navigates away.

Note that .switch/flatMap can accept a Promise or an Observable, so its a good place to show interop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions