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. DouNet学习_代码生成器

    string conn = "Data Source={0};Initial Catalog={1};User Id={2};Password={3}"; //点击链接 priva ...

  2. jboss服务器配置多实例

    jboss配置多实例的重要性 在开发, 测试项目的过程中, 我们经常需要在同一台主机上, 同一个服务器上配置多个运行实例.这样做有一下几点好处: 在项目开发, 调试阶段能最大限度的节省资源 某个实例出 ...

  3. HW7.14

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  4. linux top命令VIRT,RES,SHR,DATA的含义

    VIRT:virtual memory usage 虚拟内存1.进程“需要的”虚拟内存大小,包括进程使用的库.代码.数据等2.假如进程申请100m的内存,但实际只使用了10m,那么它会增长100m,而 ...

  5. 记拿到鹅厂前端开发暑期实习offer的经历

    #想起来时的路 在真正拿到腾讯实习offer之前,也是看过不少人的面经,心生向往.很早在入前端坑之前,我就想着大四的时候有机会要尝试去腾讯里实习. 大一入门语言就是C++,这让我很无奈,所以我很快的就 ...

  6. [原创]Devexpress XtraReports 系列 3 创建主从报表

    昨天写了系列的第二篇Devexpress XtraReports 系列 2 创建表格报表 . 今天我们来继续系列 3 创建主从报表 首先我们来看看最后实现的效果.Demo最后附上. 开始吧. 第一步, ...

  7. Ubuntu之系统交换分区Swap增加与优化

    http://os.51cto.com/art/201212/372860.htm   http://blog.csdn.net/xingyu15/article/details/5570225   ...

  8. 【LeetCode】 Subsets

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  9. GET与POST提交

    get是从服务器上获取数据,post是向服务器传送数据. get 是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,[在URL中可以看到].post是通过HTT ...

  10. (剑指Offer)面试题22:栈的压入、弹出序列

    题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等. 例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序 ...