個模式有小小似stackoverflow 噤,有人問問題, 跟住有人識答就答


希望起依個過程入面可以做倒技術交流

我先:
最近我學緊Angular JS, 當跟住Google 個Hero Editor 做到去Routing 果part 既時候就有個問題。
https://angular.io/docs/ts/latest/tutorial/toh-pt5.html
Inside the ngOnInit lifecycle hook, we use the params observable to extract the id parameter value from the ActivatedRoute service and use the HeroService to fetch the hero with that id.
我要起條URL 入面囉返一個叫做 'id' 既parameter 出黎, 噤NG2 用既ts code 就起下面
this.route.params
.switchMap((params: Params) => this.heroService.getHero(+params['id']))
.subscribe(hero => this.hero = hero); // +params['id'] = Number(params['id'])
你見倒佢中間有兩個function 叫做 'switchMap' 同 'subscriber'
依兩個function 都唔係native javascript 有既function 而係由一個叫rxjs 既library define 既
rxjs : http://reactivex.io/intro.html
我既問題有兩個:
1. 到底rxjs 有咩用? 上網睇人地 example 都唔係太明, 感覺上佢係for JS event 去做D filter , 例如double click 既event, 你就可以用filter 去define filter 兩個event 一定要相隔小過 X ms 先叫做double click, subscribe 個callback call 倒既時候就都係滿足filter 條件既event (我理解上起依個case 其實就係observer pattern , subscriber callback 就係observer, 兩個相差細過X ms 既click event 就係subject)
2. 但係我上面個例子同NG2 既應用又風馬牛不相及, 因為suppose 要讀path parameter 只需要 params['id'] 就夠,例如NG1.5 就係$routeParams.id 搞掂,所以到底依個example 點解要噤寫
