5

I have a routerLink attached to the lead value in a table I have created in the HTML of an angular2 component. Upon clicking that table value, I want the routerLink to not only send to the next component but also bring the chosen value to the next component.

Example of current code

<a [routerLink]="['/Records']" ><td> {{Member.Name}} <td></a>

I have the link so it is sending to the Records page, however how do I send the name to that component so that I can display the specific record that I am trying to display?

3 Answers 3

8

Add a route parameter to the url and then access that value using the ActivatedRoute.

Route Definition
{ path: "Records/:name", component: RecordsComponent }
Link
<a [routerLink]="['/Records', Member.Name]" ><td> {{Member.Name}} <td></a>
Get Value
@Component({
    ...
})
export class RecordsComponent implements OnInit {

    constructor(private _route: ActivatedRoute) { }

    ngOnInit(){
        this._route.params.subscribe((params) => {
        var recordName = params["name"];
        //load record data
   });
}
Sign up to request clarification or add additional context in comments.

6 Comments

What is the import that I need to use to be able to implement OnInit?
import {OnInit} from '@angular/core';
I have correctly implement OnInit and am no longer getting an error, however, I am getting another error that says it cannot find the name ngOnInit.
Yes that did it! Now just one more question: is there a way that I can use that recordName variable outside of the ngOnInIt function? (I am trying to send that name to an api call function in the same component)
Thank you, Teddy, I am forever in debt to you.
|
4

define your route like this:

{ path: 'Records/:id', component: xxx }

declaratively:

<a [routerLink]="['/Records', Member.name]">{{ Member.name }}</a>

programmatically:

goToMemberDetails(name) {
   this.router.navigate(['/Records', name]);
 }

Comments

0
@Component({
    ...
})
export class RecordsComponent implements OnInit {

    constructor(private _route: ActivatedRoute) { }

    ngOnInit(){
        this._route.params.subscribe((params) => {
        var recordName = params["name"];
        //load record data
   });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.