是否可以通过异步管道调用可观察类上的方法?
我已经尝试了以下方法,但无法使其工作。所以我想知道这是否可能或可以以更好的方式完成?
app.component.html
<p>Total Cost: {{ (orderState$ | async).TotalPrice() }}</p>
order.state.ts
export class OrderState {
lineItems: OrderLineItem[];
public TotalPrice(): number
{
// TODO: Calculate price of line items
return 199;
}
}
最佳答案
如有必要,创建另一个可观察对象。最好在 subscribe
中设置一个变量。
export class OrderState {
lineItems: OrderLineItem[];
get totalPrice(): number {
// TODO: Calculate price of line items
return 199;
}
}
export class TheComponent {
orderState$: Observable<OrderState>;
// Here
totalPrice$: Observable<string>;
constructor() {
// And then after you know what the orderState is.
this.totalPrice$ = this.orderState$.map(state => state.totalPrice);
}
}
<p>Total Cost: {{totalPrice$ | async}}</p>
关于angular - 可以从异步管道调用类方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45662086/