Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Passing object to @Input parameter with routerlink

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.

like image 531
Don Chambers Avatar asked Dec 15 '17 16:12

Don Chambers


People also ask

Can we give routerLink to div?

Yes it can be attached to div tag, your route is probably wrong try add / in front of route.

What is difference between routerLink and routerLink?

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.

How do I use routerLink in anchor tag?

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.


2 Answers

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 }
like image 167
Babar Siddiqui Avatar answered Sep 20 '22 13:09

Babar Siddiqui


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 }
like image 36
Baumi Avatar answered Sep 17 '22 13:09

Baumi