如何在不触发新发射的情况下获取可观察值

doe*_*doe 1 rxjs typescript angular

我如何获得可观察值的发射值而又不触发新发射?我想在ngClass标记中获取可观察值。

我尝试使用管道水龙头来获取通过switchMap传递的值,但没有记录该值。然后,我可以使用trueIfDefinitionsLengthngClass条件。

this.definitions$.pipe(
  tap(value => console.log('timer result =', value))
)
Run Code Online (Sandbox Code Playgroud)

观察者订阅| 异步的

模板:

<input [ngClass]="{'input-has-results': trueIfDefinitionsLength > 0}"
       [formControl]="searchInput">

<ng-container *ngIf="definitions$ | async as definitions">
  <div class="format-options" *ngIf="definitions.length > 0">
    <div *ngFor="let definition of definitions" class="search-result">
      <div>{{definition.name}}</div>
      <div>{{definition.description}}</div>
    </div>
  </div>
</ng-container>
Run Code Online (Sandbox Code Playgroud)

零件

this.definitions$ = this.searchInput.valueChanges
                        .pipe(
                            tap(value => console.log('input')),
                            //startWith(''),
                            debounceTime(500),
                            //distinctUntilChanged(),
                            switchMap(value => this.definitionService.searchTerm(value))
                        );
Run Code Online (Sandbox Code Playgroud)

Nic*_*s K 5

使用BehaviorSubject代替。它具有一种getValue()在不发出新值的情况下获取当前值的方法。