Home > Software engineering >  Share variable between angular component
Share variable between angular component

Time:03-22

I have a list of items. The user needs to click on one item to open a new site (new component). In the new component, I need the id of the item the user selected to calculate the specific result.

The following code shows the list of items where the user needs to click.

<div >

<div *ngFor="let item of items"> <mat-card id="{{item.id}}" [routerLink]="['/', 'subject', subjectID, 'result', item.id]">

<span>{{item.name}}</span>

</mat-card> </div>

When the user clicks an item, the new component opens correctly. For example the URL looks like localhost:4200/subject/2/result/5 What I want to do is to access the '5' of the URL in the called component. I tried to access it via a subscription of params like:

this.route.params.subscribe(params) => { this.itemID = params('item.id'); } );

It shows the error TS2304: Cannot find name 'params'. I think it's because I'm using the id as a path and not as a parameter. Do you have any advice for me how I can resolve my problem?

CodePudding user response:

Have you tried like this?

ngOnInit() {
  this.route.queryParams.subscribe(params => {
   this.id = params['id'];
  });
}

But share your route config, please :)

  • Related