student18
student18

Reputation: 538

Async pipe in Angular 6, how to store object in components variable without subscribe()

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

Upvotes: 0

Views: 986

Answers (2)

Artyom Amiryan
Artyom Amiryan

Reputation: 2966

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

Upvotes: 1

Poul Kruijt
Poul Kruijt

Reputation: 71961

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

Upvotes: 1

Related Questions