Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to keep the value from ngFor on click for another component

I’m currently working on an angular project and I need some help with this problem…

I displayed the data from an array and next I need to save the value from the element I click so I can display it on another component and play with this value with an API link

This is the code from the component where I display data

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div class="popup-content">
<ul class="list-group">
    <li  mat-dialog-close
        routerLink='/admin'
        *ngFor="let t of data"
        class="list-group-item">
        {{t}}
      
</li>
</ul>
</div>

How can I save the value of ‘t’ on click so I can use it on another component

The data array is created like this

this.service.getPosts()
      .subscribe(response => {
        this.posts = response;
        this.num = this.posts.body.paginationinfo.numberofelementsTotal;

        for(this.i=0;this.i<this.num;this.i++)
        {
          this.test.push(this.posts.body.listOfUnapprovedChangeRequests[this.i].requestuuid);
        }
        
      });

      console.log(this.test);
  }

  openDialog(){
    this.dialogRef.open(MenupopupComponent,{data:this.test})
  }

>Solution :

You can pass it as a query param.

<ul class="list-group">
  <li
    mat-dialog-close
    routerLink="/admin"
    queryParams="{ requestId: t }"
    *ngFor="let t of data"
    class="list-group-item"
  >
    {{ t }}
  </li>
</ul>

Then read that requestId query parameter with the help of the ActivatedRoute:

export class AdminComponent {
  constructor(private route: ActivatedRoute) {}

  ngOnInit(): void {
    const requestId = this.route.snapshot.paramMap.get('requestId');
    console.log('requestId', requestId);
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading