You can load resource based on the url using the a combination of ActivatedRouteand 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}`
}))
}
}

Github

[Angular2 Router] Load Data Based on Angular 2 Route Params的更多相关文章

  1. [Angular2 Router] Resolving route data in Angular 2

    From Article: RESOLVING ROUTE DATA IN ANGULAR 2 Github If you know Anuglar UI router, you must know ...

  2. LOAD DATA INFILE – performance case study

    转: http://venublog.com/2007/11/07/load-data-infile-performance/ I often noticed that people complain ...

  3. [Angular2 Router] CanDeactivate Route Guard - How To Confirm If The User Wants To Exit A Route

    In this tutorial we are going to learn how we can to configure an exit guard in the Angular 2 Router ...

  4. mysql load data 乱码

    解决方案: http://stackoverflow.com/questions/26256421/sql-load-data-infile-utf8-issue 即: load data local ...

  5. Mybatis拦截器 mysql load data local 内存流处理

    Mybatis 拦截器不做解释了,用过的基本都知道,这里用load data local主要是应对大批量数据的处理,提高性能,也支持事务回滚,且不影响其他的DML操作,当然这个操作不要涉及到当前所lo ...

  6. mysql load data 乱码的问题

    新学mysql在用load data导入txt文档时发现导入的内容,select 之后是乱码,先后把表,数据库的字符集类型修改为utf8,但还是一样,最后在 http://bbs.chinaunix. ...

  7. mysql load data infile的使用 和 SELECT into outfile备份数据库数据

    LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt' [REPLACE | IGNORE] INTO TABLE t ...

  8. 快速的mysql导入导出数据(load data和outfile)

    1.load data: ***实际应用:把日志生成的xls文件load到MySQL中: mysql_cmd = "iconv -c -f utf-8 -t gbk ./data/al_ve ...

  9. 记录load data infile 的用法

    load data local infile 'd:/1.txt' into table tcm.wm_dis_category fields terminated by';' lines termi ...

随机推荐

  1. html --- SVG --- javascript --- 旋转矩形

    可缩放矢量图形(英语:Scalable Vector Graphics,SVG)是基于可扩展标记语言(XML), 用于描述二维矢量图形的一种图形格式.SVG由W3C制定,是一个开放标准. 在 Inte ...

  2. 封装cookie.js、EventUtil.js、

    最近学习了javascript,封装好的东西看起来舒服,以备需要的时候拉出来,jquery对javascript做了很好的封装!以后会多用jquery多些 var CookieUtil = { get ...

  3. 本地虚拟机挂载windows共享目录搭建开发环境

    关闭防火墙(本地环境 直接关掉即可)service iptables stop检查是否安装了需要的samba软件包rpm –q samba如果没安装yum install samba system-c ...

  4. 获取子窗口中使用jQuery.data()设置的参数

    http://hyj1254.iteye.com/blog/643035 假设在iframe子窗口中设置了$('#mydata').data('key','hello world'); 那在包含ifr ...

  5. java&c# dec 加密,通用

    java /** * 解密DES * @param key 密钥,长度必须是8的倍数 * @param data 数据源 * @return 解密内容 */ public final static S ...

  6. 第三百四十八天 how can I 坚持

    回来的倒不晚,算了不想抱怨了. 晚上回来吃过饭,又看了遍<活着>,把一切悲剧都放在一个人身上了,很朴实,好感人. 一天就写了一个借口,也是醉了. 我的天气预报,我的struts.sprin ...

  7. 将表A的数据复制到表B,以及关于主表和子表的删除办法

    如果表A的数据结构和表B的数据结构是一样的,字段名字可以不用相同,但是对应的数据类型是一样的 这样的情况下可以用如下的方式实现将表A的数据复制到表B INSERT INTO #TEMP2 SELECT ...

  8. Android打开WIFI或者移动网络的代码实现

    MainActivity如下: package wy.testnetwork; import java.lang.reflect.Field; import java.lang.reflect.Inv ...

  9. DP练习(概率,树状,状压)

    http://vjudge.net/contest/view.action?cid=51211#overview 花了好长时间了,终于把这个专题做了绝大部分了 A:HDU 3853 最简单的概率DP求 ...

  10. HDU 3920Clear All of Them I(状压DP)

    HDU 3920   Clear All of Them I 题目是说有2n个敌人,现在可以发n枚炮弹,每枚炮弹可以(可以且仅可以)打两个敌人,每一枚炮弹的花费等于它所行进的距离,现在要消灭所有的敌人 ...