2

I have a parent component and a child component. I was trying to navigate to child component using route.navigate based on button click. Is it possible to pass an object with route.navigate to the child component?

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
Anna
  • 337
  • 1
  • 3
  • 9
  • it is not possible to pass an object through router. you need to create a service and set the value in that and get it from another component – Anto Antony Nov 15 '17 at 04:49
  • 2
    Possible duplicate of [How do I pass data to Angular routed components?](https://stackoverflow.com/questions/36835123/how-do-i-pass-data-to-angular-routed-components) – Aamir Khan Nov 15 '17 at 04:49

1 Answers1

1

In my case I navigate from a material table to a form and pass a raw id (it is possible to pass all the raw object)

Here's my code :

<mat-cell id="idVtitle" *cdkCellDef="let row">
  <button id="idValidationScreenButton" mat-button [routerLink]="['/validation', row.documentid]">{{row.title}}
  </button>
</mat-cell>

And in my ValidationComponent refering to the rout validation I have something like this:

  constructor(private _route: ActivatedRoute,
              private _router: Router,) {
  }

And then calling this this._route.params.map((params: any) => params.id) to get the param (in my case I use the param to call the backend)

private fetchDocument() {
    this._route.params
      .map((params: any) => params.id)
      .flatMap((id: string) => this._sharedService.fetchOneDocument(id))
      .subscribe((doc: any) => this.docToValidate = doc);
  }

I hope this could help.

H.abidi
  • 579
  • 1
  • 7
  • 16