[英]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.