回答
嘗試使用路由器參數。因此,在您的路由器配置中,有像這樣的組件的路由器路徑(被視爲「MyComponent」);
routes: Routes = [ { path: 'mycomponent/:myParam', component: MyComponent} ]
。
然後將實際值傳遞給HTML中錨點標記中的路由器,如:
<a [routerLink]="['/mycomponent', paramValue]"> Link to MyComponent </a>
或在你的父組件,導航到編程MyComponent的;
this.router.navigate(['/mycomponent', paramValue]);
而在你的組分(MyComponent的),從角度路由器,它提供了一個PARAMS可觀察導入ActivatedRoute,以及訂閱該可觀察到的在ngOnInit方法。所以你的MyComponent應該看起來像這樣;
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'my-selector',
template: '<div>My Component - Param Value {{myParamValue}}</div> '
})
export class MyComponent implements OnInit, OnDestroy{
myParamValue: any;
private subscribe: any;
constructor(private route: ActivatedRoute){}
ngOnInit() {
this.subscribe= this.route.params.subscribe(params => {
this.myParamValue = params['myParam'];
});
}
ngOnDestroy() {
this.subscribe.unsubscribe();
}
}
更多here
這與屬性綁定相同嗎?所以在這個例子中,你正在發送myParamValue到激活的路由器鏈接?到你目前的路線在哪裏? –
當您具有應該綁定到組件屬性的模板屬性值時,應用屬性綁定。如果您必須傳遞給組件的數據類似於ID,則可以使用此方法。否則,請使用通用服務在這些組件之間共享數據 – HashanMadz
- 1. Angular 2組件路由
- 2. angular 2 es5組件路由
- 3. Angular 2 |我不能綁定另一個類的屬性
- 4. Angular/2 NG/2:ngModel - 綁定到還不存在的屬性
- 5. Angular 2屬性綁定。有點工作
- 6. Angular 2動態HTML與屬性綁定
- 7. 綁定到對象的屬性在Angular 2中不起作用
- 8. 綁定不能在Angular 2組件中工作
- 9. 角2:父子組件屬性綁定
- 10. Angular 2不能匹配路由錯誤
- 11. Webapi 2屬性路由不起作用
- 12. 我自己的Angular 2表格組件 - 2路數據綁定
- 13. Angular 2路由不會路由
- 14. $路由器沒有綁定在Angular 1.5組件
- 15. AngularJS/Restangular路由「不能設置屬性'路由'未定義」
- 16. 屬性指令不能綁定到主機組件屬性
- 17. Angular 2 - 在路由器出口上顯示一個組件
- 18. Web API 2路由 - 路由屬性
- 19. 在Angular-2中使用Ui路由
- 20. 組件間事件綁定Angular 2
- 21. Angular 2使用ng2-bootstrap工具提示的屬性綁定
- 22. 使用Angular 2綁定到模式屬性
- 23. 綁定 '&' 在路由組件方法
- 24. 沒有路由的組件的延遲加載路由。 Angular 2
- 25. Angular 2路由不能匹配任何路由
- 26. Angular 2 - 從路由器插座生成訪問子組件屬性
- 27. Angular 2兒童路由(v3)'不能讀取'undefined'的屬性'註釋'
- 28. 綁定在組件路由器 - 角1.5
- 29. Angular 2 - 不能導航到直接使用URL的路由
- 30. Angular 2路由,.htaccess
你可以分享你的代碼?雖然有人已經給出了答案,但如果您可以提供代碼 – brijmcq
是什麼讓您認爲它不起作用,那將會更有幫助? –
你可以參考我的另一個問題。我想要的是我想要將類屬性綁定到子組件。 https://stackoverflow.com/questions/44817467/angular-2-i-cant-bind-property-from-another-class?noredirect=1#comment76615953_44817467 –