0

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 :)

1
  • Any issue you are facing with above code? Commented Oct 16, 2018 at 14:15

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.