我下的路由/布拉布拉/創建組件:ID /布拉布拉/:secondidAngular2:得到母路由器數據
我的網址的第一部分 「/布拉布拉/:ID/...」 由管理我的第一個路由組件。
然後,第二個路由器管理url的第二部分。
在我的組件中,我可以輕鬆訪問RouteData中的「:secondid」。
但我想知道如何從那裏訪問我的「:id」。
有什麼想法?
我下的路由/布拉布拉/創建組件:ID /布拉布拉/:secondidAngular2:得到母路由器數據
我的網址的第一部分 「/布拉布拉/:ID/...」 由管理我的第一個路由組件。
然後,第二個路由器管理url的第二部分。
在我的組件中,我可以輕鬆訪問RouteData中的「:secondid」。
但我想知道如何從那裏訪問我的「:id」。
有什麼想法?
通過第一個id
以及數據綁定。
如何在路由時將數據綁定到組件? 第二個路由器看起來像這樣 @Component({... template:'
KI認爲我在那裏找到了我的答案:https://github.com/angular/angular/issues/4452 –
您必須使用Injector
來獲取家長數據。
這是使用該功能從here中提取的示例代碼。
this.params = injector.parent.parent.get(RouteParams);
this.userLogin = this.params.get('userLogin');
看看構造以及如何injector
用於獲取之母的userLogin
import {Component, Injector} from 'angular2/core';
import {Http} from 'angular2/http';
import {ROUTER_DIRECTIVES, Router, RouteParams, RouteConfig} from 'angular2/router';
@Component({
directives: [ROUTER_DIRECTIVES],
template: `
<div class="col-sm-3">
<img class="img-circle" src="" />
<p *ngIf="userData.name">
<i class="glyphicon glyphicon-user"></i>
</p>
<p *ngIf="userData.company">
<i class="glyphicon glyphicon-briefcase"></i>
</p>
<p *ngIf="userData.location">
<i class="glyphicon glyphicon-globe"></i>
</p>
</div>
<div class="col-sm-9">
</div>
`,
styles: [`
img { width: 100px; margin-bottom: 10px; }
`]
})
export class UserDetail {
params: RouteParams;
userLogin: string;
userData: Object = {};
constructor(public http: Http, params: RouteParams, injector: Injector, private _router: Router) {
// We use injector to get a hold of the parent's params
this.params = injector.parent.parent.get(RouteParams);
this.userLogin = this.params.get('userLogin');
}
ngOnInit() {
this.http.get(`http://api.github.com/users/${this.userLogin}`)
.map(response => response.json())
.subscribe(
data => this.userData = data,
err => console.log(err)
);
}
}
這似乎不再適用[Angular 2.0.0-beta.16(2016-04-26)] (https://github.com/angular/angular/blob/master/CHANGELOG.md#breaking-changes-1) – Sleeper9
簡單的用'RouteParams'送'id'而路由 –
我怎樣才能做到這一點?在我的第二個路由器中,我可以訪問我的構造函數中的routeParams以瞭解第一個:id的值。但是,我怎麼能傳播這個到我的路線? –