0
votes

consider following code snippet where I subscribe an Observable using async pipe in a components template:

<table>
  <tr *ngFor="let s of data | async">
    <td>some informations</td>
    <button (click)="setObject(s)">Save Object s in components variable</button>
  </tr>
</table>

My question is if there is a way to store the object retrieved from *ngFor in a components local variable? I know there is a way to store it in a templates variable, but I want to save it in a components variable.
So for example, if I click a button, a components setter method is called to store the object in a components variable of same type.

Many thanks in advance :)

2
Any issue you are facing with above code? - Dheeraj Kumar

2 Answers

1
votes

You should create a component member and assign the s to it:

export class BlaBlComponent {
  clickedObject;
}

and do this in your template:

<button (click)="clickedObject = s">Click</button>

This will store the s reference to the clickedObject member on your component. This is quite basic angular stuff, and I advise you to thoroughly do the tutorials and read the documentation at angular.io. Or maybe I'm not getting the nature of your question

1
votes

here is example

<table>
  <tr *ngFor="let s of (data | async) as dt">
    <td>some informations</td>
    <button (click)="setObject(dt)">Save Object s in components variable</button>
  </tr>
</table>

also stackBlitz link here