I have an angular component with a @Input parameter as follows.
@Component({
selector: 'app-transmission-history'
})
export class TransmissionHistoryComponent implements OnInit {
@Input() theRecord: Record = null;
constructor(private _transmissionHistoryService: TransmissionHistoryService) { }
}
In another component I have this in the markup:
<app-transmission-history [theRecord]="selectedRecord"></app-transmission-history>
Perfect. Everything works as expected.
I need to move the app-transmission-history component to a stand alone page. Now I need to access it with routerLink as follows:
<a routerLink='/transmissionHistory'>{{selectedRecord.Name}}</a>
I can't figure out how to set the @Input parameter in the routerlink in the markup. The value I need to set it to is selectedRecord, which I have access to.
I could make changes to the app-transmission-history component to receive the parameter another way. However, it does not seem flexible if I have to change the implementation based on how we get to it.
Yes it can be attached to div tag, your route is probably wrong try add / in front of route.
What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.
Adding routerLink attribute in anchor tag. Therefore we will use routerLink to achieve the same functionality as Susper is a single page application, where the page should not be reloaded. routerLink navigates to the new url and the component is rendered in place of <router-outlet> without reloading the whole page.
Use query parameter to pass object after converting to json string.
<a [routerLink]="['/transmissionHistory']"
[queryParams]="{record:selectedRecord | json }">
{{selectedRecord.Name}}
</a>
In your component you can do something like this to read params:
@Component({
selector: 'app-transmission-history'
})
export class TransmissionHistoryComponent implements OnInit {
theRecord: Record = null;
constructor(
private _transmissionHistoryService: TransmissionHistoryService,
private _route: ActivatedRoute
) { }
ngOnInit() {
this._route.params.subscribe(params => {
this.theRecord = JSON.parse(params.record) as Record;
});
}
}
And router config will look like as:
{ path: 'transmissionHistory', component: TransmissionHistoryComponent }
Please take a look at router parameters. You can pass you param like this:
<a [routerLink]="['/transmissionHistory', selectedRecord]">{{selectedRecord.Name}}</a>
Than in you component you can retrive it like this:
@Component({
selector: 'app-transmission-history'
})
export class TransmissionHistoryComponent implements OnInit {
theRecord: Record = null;
constructor(
private _transmissionHistoryService: TransmissionHistoryService,
private _route: ActivatedRoute
) { }
ngOnInit() {
this._route.params.subscribe(params => {
this.theRecord = params['record'];
});
}
}
Remember to setup this parameter in your router config:
{ path: 'transmissionHistory/:record', component: TransmissionHistoryComponent }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With