简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM