这个问题是基于https://github.com/ParthibanRajalingam/demos中的一个例子.
Only the initial values of the stock object are displayed using app.component.html:
<div class="card" *ngIf="stock$ | async; let stock">
<div class="textBox">
<div class="textContent">
<div class="h1">{{stock.name}}<div *ngIf="stock.increased"
style="color: green ; margin: 0 10px 0 10px; float: right;">▲</div>
<div *ngIf="!stock.increased" style="color: red ; margin: 0 10px 0 10px; float: right;">▼</div>
</div>
</div>
<div class="detail">Price: ${{stock.price}}
</div>
</div>
</div>
The app.component.ts uses BehaviorSubject and Observable from rxjs:
private webSocket: WebSocket;
private subscription: Subscription = Subscription.EMPTY;
public stock: Stock = new Stock();
public stockSubject: BehaviorSubject<Stock> = new BehaviorSubject<Stock>(this.stock);
public stock$: Observable<Stock> = this.stockSubject.asObservable();
this.webSocket = new WebSocket('ws://localhost:8080/stocks');
this.subscription = this.stock$.subscribe();
this.webSocket.onmessage = (event) => {
this.stock = JSON.parse(event.data.toString());
this.stockSubject.next(this.stock);
console.log(JSON.stringify(this.stock));
};
The Stock object has public attributes to avoid using the any type:
export class Stock {
name: string;
icon: string;
price: number;
increased: boolean;
constructor () {
this.name = 'Amazon';
this.icon = '';
this.price = 0;
this.increased = false;
}
}
ANGLE应用程序是一个Web服务客户端,它接收更新的股票价格并在BehaviorSubject上调用next:
{"name":"Amazon","icon":"https://cdn.cdnlogo.com/logos/a/77/amazon-dark.svg","price":12.17,"increased":false}
{"name":"Amazon","icon":"https://cdn.cdnlogo.com/logos/a/77/amazon-dark.svg","price":12.72,"increased":true}
{"name":"Amazon","icon":"https://cdn.cdnlogo.com/logos/a/77/amazon-dark.svg","price":12.71,"increased":false}
在HTML页面上显示更新的股票属性缺少什么?最初的例子没有使用对象或观察物,对作者来说是有效的.