Search code examples
angulartypescriptbuttoncomponentsrouterlink

Passing an object's string value to component using a button Angular


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'.


Solution

  • 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') ;
         })
    

    }