简体   繁体   English

如何从模板到.ts 共享一个 let 值(在 *ngFor 内)

[英]How to share a let value (inside *ngFor) from template to .ts

I would like to take the work of pipe async to share the interpolation value {{ticketType.name}} with the.ts component to work with this value.我想利用 pipe 异步的工作与 .ts 组件共享插值 {{ticketType.name}} 以使用此值。 Is it in fact possible?事实上可能吗?

Here the template code:这里是模板代码:

<mat-option *ngFor="let ticketType of ticketTypesQuery.list$ | async" [value]="ticketType.id">
       {{ticketType.name}}
</mat-option>

The question isn't exactly clear at the moment, but you could always pipe in any RxJS operator to the source observable in the component controller (*.ts) to use it's notifications.这个问题目前还不是很清楚,但你总是可以在任何 RxJS 运算符中的 pipe 到组件 controller 中可观察到的源(*。

For eg.例如。 if you only wish to use the value emitted from the observable without modifying it, you could use the tap operator.如果你只想使用从 observable 发出的值而不修改它,你可以使用tap操作符。 Try the following尝试以下

Controller (*.ts) Controller (*.ts)

import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

export SomeComponent implements OnInit {
  list$: Observable<any>;

  constructor() { }

  ngOnInit() {
    this.list$ = this.ticketTypesQuery.list$.pipe(
      tap(ticketTypes => {
        // use `ticketTypes` array emission from the observable
      })
    );
  }
}

Template (*.html)模板 (*.html)

You could also wrap the async pipe in <ng-container> to reuse it's emissions without another async .您还可以将async pipe 包装在<ng-container>中,以便在没有另一个async的情况下重用它的排放。 Note that each async pipe will trigger an individual subscription of a cold observable.请注意,每个async pipe 都会触发对冷可观察对象的单独订阅。

<!-- Note: we're using `list$` defined in the controller, not `ticketTypesQuery.list$` -->

<ng-container *ngIf="(list$ | async) as list">
  <mat-option *ngFor="let ticketType of list" [value]="ticketType.id">
    {{ticketType.name}}
  </mat-option>
</ng-container>

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

相关问题 如何在ngfor中将变量从模板传递到ts - How to pass variable from template to ts in ngfor 如何从Typescript中的* ngFor =“让英雄英雄”中引用模板输入变量&#39;hero&#39; - How can you reference template input variable 'hero' from *ngFor=“let hero of heros” inside Typescript Angular 8:如何将值从 *ngfor 发送到 TS 文件 - Angular 8 : How to send the value from *ngfor to TS file 如何在 formGroup 中从 typescript 传递自定义字符串,并在模板内的 *ngFor 中读取它的值? - How do I pass down a custom string from typescript, in the formGroup, and read the value of it in *ngFor inside template? 如何将模板中的值传递给 ts function - How to pass a value from template to ts function 如何从下拉列表和 *ngFor 中创建的文本中获取 component.ts 中的输入值 - How to get input value in component.ts from dropdown and text created within *ngFor in table 从 ngFor Angular 将值从 HTML 传递到 TS - pass value from HTML to TS from an ngFor Angular Angular 2:如何让* ngIf使用* ngFor数组中包含的值? - Angular 2 : How to let *ngIf use value from array of *ngFor which includes it? 如何在Angular 2中将* ngFor = let值用作html属性值 - How to use the *ngFor=let value as html property value in angular 2 如何让component.ts在加载模板之前加载数据? - How to let component.ts load data before template is loaded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM