[Angular2 Router] Load Data Based on Angular 2 Route Params
2023-09-14 09:00:53 时间
You can load resource based on the url using the a combination of ActivatedRoute
and Angular 2’s Http
service. Since the params and Http
are both streams, you can use RxJS to get the data off the param then switchMap
over to an Http
request using that data.
Hero.component.ts:
import { Component, OnInit } from '@angular/core'; import {ActivatedRoute} from "@angular/router"; import {StarWarsService} from "../heros.service"; import {Observable} from "rxjs"; @Component({ selector: 'app-hero', templateUrl: 'hero.component.html', styleUrls: ['hero.component.css'] }) export class HeroComponent implements OnInit { hero: Observable<Object>; constructor(private router: ActivatedRoute, private starwarService: StarWarsService) { this.hero = router.params.map((p:any) => p.id) .switchMap( id => this.starwarService.getPersonDetail(id)) .startWith({ name: 'Loading...', image: '' }) } ngOnInit() { } }
hero.component.html:
<div> <h2>{{(hero | async)?.name}}</h2> <img [src]="(hero | async)?.image" [alt]="(hero | async)?.name"> <!-- Notice that, here we use ? mark. This is not necessary if we use 'startWith({name: '', image: ''})' startWith will set init value, so that hero.name / hero.image won't be undefined --> </div>
heros.service.ts:
import {Injectable, Inject} from '@angular/core'; import {STARWARS_BASE_URL} from "../shared/constance.service"; import {Http} from "@angular/http"; import "rxjs/add/operator/map"; import "rxjs/add/operator/switchMap"; @Injectable() export class StarWarsService { constructor(@Inject(STARWARS_BASE_URL) private starwarUrl, private http: Http ) {} getPeople(){ return this.http.get(`${this.starwarUrl}/people`) .map( res => res.json()) } getPersonDetail(id){ return this.http.get(`${this.starwarUrl}/people/${id}`) .map( res => res.json()) .map( (hero:any) => Object.assign({}, hero, { image: `${this.starwarUrl}/${hero.image}` })) } }
相关文章
- Laravel的Blade模板引擎与Angular均使用“{ {}}”标签冲突
- [Angular] Use Angular components in AngularJS applications with Angular Elements
- [Angular] Dynamic component rendering by using *ngComponentOutlet
- [Angular + TsLint] Disable directive selector tslint error
- [Angular] Style HTML elements in Angular using ngStyle
- [Angular] Show a loading indicator in Angular using *ngIf/else, the as keyword and the async pipe
- [Angular] Create custom validators for formControl and formGroup
- [Angular 2] Set Values on Generated Angular 2 Templates with Template Context
- [Angular 2] Value Providers & @Inject
- [Angular 2] More on *ngFor, @ContentChildren & QueryList<>
- [Angular] Use :host-context and the ::ng-deep selector to apply context-based styling
- [Angular 2] Set Values on Generated Angular 2 Templates with Template Context
- [Angular 2] Set Properties on Dynamically Created Angular 2 Components
- [Angular 2] More on *ngFor, @ContentChildren & QueryList<>
- [Angular] oc.lazyLoad with Angular ui router
- Angular应用i18n - internationalization翻译的实现单步调试
- 关于 index.ts 在大型 Angular 项目中的应用
- Angular 如何根据一个 class 的定义和数据,动态创建一个该类的实例
- Angular InjectionToken的一个具体使用例子
- Angular jasmine单元测试框架里expect.toHaveBeenCalled的工作原理