如何比较由类型为 Kind
的用户 inputKind 选择的类型。 randomKind$
的类型为:Observable<Kind>
?
HTML:
<div class="container-fluid">
<div class="row justify-content-center">
<ng-container *ngIf="((kindViewModel$ | async)?.kinds) as kinds">
<div>
<div>
<h1>{{(randomKind$ | async).originalText}}</h1>
</div>
<div *ngFor="let kind of kinds">
<h3>
<button mat-button (click)="kindSelected(kind)">{{kind.translatedText}}</button>
</h3>
</div>
</div>
</ng-container>
</div>
</div>
打字稿:
public randomKind$: Observable<Kind> = this.kindsService.kindViewModel$
.pipe(map(t => t.kinds[this.randomInt(0, 7)]));
public kindSelected(inputKind: Kind) {
//how to compare inputKind with randomKind?
}
export interface Kind {
originalQuestions: string;
originalText: string;
translatedQuestions: string;
translatedText: string;
}
答案 0 :(得分:0)
订阅 randomKind$
并监听值。将最新值存储在另一个属性中。让我们将其命名为 theVal
this.randomKind$.subscribe((x) => { this.theVal = x });
与新的 theVal
进行比较。
另一种可能是BehaviorSubject。由于它具有 getValue
方法,因此您可以随时读取最新值。