I have a toolbar application that consists of a treeview component (which lists the various content nodes) and a dashboard editing component that displays some editable content depending on which tree branch is selected.
eg. The tree is as follows:
- Football - - Premier League - - - Arsenal - - - Chelsea - - - ...etc - - Championship - - - Derby - - - ...etc
You click Arsenal in the tree and it displays some content for this command in an editable panel on the page.
The component that displays the subcomponents is as follows:
@Component({ selector: 'my-dashboard', template: ` <div class="tree-panel-container"> <div class="tree-panel-content"> <content-tree [startNodeId]="startNodeIdContent"></content-tree> </div> </div> <router-outlet></router-outlet> `, directives: [ ContentTreeComponent, ContentDashboardComponent, RouterOutlet ], providers: [ HTTP_PROVIDERS ] })
Editable content is displayed in the router-outlet , so that each editable piece of content has its own separate URL, for example. example.com/content/edit/123 where 123 is the identifier of the content of the Arsenal, for example.
It all works great.
However, what I want to do is access the id route parameter in the content-tree component. Currently, I am sure that the code that I have in this component should work:
import {Component, Input, OnInit} from '@angular/core'; import {Router, RouteParams} from '@angular/router-deprecated'; import {ContentNode} from './content-node'; import {ContentService} from '../services/content.service'; @Component({ selector: 'content-tree', directives: [ContentTreeComponent], template: ` <ol class="tree"> <li *ngFor="let contentNode of contentNodes" class="tree__branch" [ngClass]="{'tree__branch--has-children': contentNode.HasChildren}"> <a *ngIf="contentNode.HasChildren" (click)="contentNode.toggle=!contentNode.toggle" class="tree__branch__toggle"> {{ !!contentNode.toggle ? '-' : '+' }} </a> <a class="tree__branch__link" (click)="onSelect(contentNode)">{{ contentNode.Name }}</a> <content-tree *ngIf="contentNode.toggle" [startNodeId]="contentNode.Id"></content-tree> </li> </ol> <div class="error" *ngIf="errorMessage">{{errorMessage}}</div> ` }) export class ContentTreeComponent implements OnInit { constructor( private _contentService: ContentService, private _router: Router, private _routeParams: RouteParams ) { } errorMessage: string; @Input('startNodeId') private _startNodeId: number; contentNodes: ContentNode[]; ngOnInit() { let nodeId = +this._routeParams.get('id'); console.log('nodeId = ' + nodeId); this.getContentNodes(); } onSelect(contentNode: ContentNode) { this._router.navigate( ['ContentEdit', { id: contentNode.Id }] ); } getContentNodes() { this._contentService.getContentNodes(this._startNodeId) .subscribe( contentNodes => this.contentNodes = contentNodes, error => this.errorMessage = <any>error ); } }
But the nodeId variable in the nodeId method always returns as 0 .
Questions: Is it possible to only access the route parameters in the component displayed by the router? If so, then the best way to handle this is to create a second (named because there will now be 2) router-exit? If not, what am I doing wrong?
Many thanks.
EDIT:
Worker (and very ugly;)) Now Plnkr was created to show the basics of the application: http://plnkr.co/edit/W3PVk3Ss5Wq59IbnLjaK?p=preview . See comments on what should happen ...