-1

In my html template of my component 'a', I have a button which is used to navigate to an other component :

<button nbButton status="info" class="left" [routerLink]="['/centers', center.id, 'prices']">PRICES</button>

So clicking on this button made me move to an other component 'b'.

In the component 'a' I have a private object variable, which contains a string value currenciesAccepted, basically :myObject.currenciesAccepted.

In the component 'b', I need this string value, so I need to pass it when I click on the button, to navigate from the component 'a' to the component 'b'.

Fealo
  • 99
  • 1
  • 4
  • 14

2 Answers2

1

try this

<button nbButton status="info" class="left" 
[routerLink]="['/centers', center.id, 'prices']" 
[queryParams]='{currenciesAccepted: myObject.currenciesAccepted}' >PRICES</button>

in centers component

import { ActivatedRoute } from '@angular/router';

currenciesAccepted: any = {};
constructor( private route: ActivatedRoute) { }

ngOnInit() {
    this.route.queryParamMap.subscribe( params => {
     this.currenciesAccepted  = params.get('currenciesAccepted') ;
     })

}

Ghoul Ahmed
  • 4,446
  • 1
  • 14
  • 23
0

You can add [queryParams]='{"obj":myObject.currenciesAccepted}' to the button, this will pass the string in query params

Sachin Gupta
  • 4,981
  • 3
  • 16
  • 32
  • Ok, but how to get this obj on the component 'b' ? – Fealo May 29 '19 at 10:30
  • The ActivatedRoute Observable contains the query parameters. You can't access a private variable from the template though, so either switch it to public or create a getter method. – Will Alexander May 29 '19 at 10:32