简体   繁体   English

Angular 6中的异步管道,如何在不使用subscribe()的情况下将对象存储在组件变量中

[英]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: 考虑以下代码片段,其中我在组件模板中使用异步管道订阅了Observable:

<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? 我的问题是,是否有一种方法可以将从* ngFor中检索到的对象存储在组件局部变量中? 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: 您应该创建一个组件成员并将s分配给它:

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. 这会将s引用存储到组件上的clickedObject成员。 This is quite basic angular stuff, and I advise you to thoroughly do the tutorials and read the documentation at angular.io . 这是非常基础的角度知识,我建议您彻底进行教程并阅读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 stackBlitz链接在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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