简体   繁体   English

Angular2搜索将布尔值添加到地图valueChanges

[英]Angular2 search add boolean to map valuechanges

this.searchField.valueChanges
      .debounceTime(400)
      .distinctUntilChanged()
      .switchMap(term => this.data(term), this.variable = true)
      .subscribe((result) => {
        this.variable = false;

      });

In the above code i want to make the this.variable to true when the this.data observable method is being called. 在上面的代码中,我想在调用this.data observable方法时将this.variable为true。 This is the way i tried but it gives error that it's not a observable method 这是我尝试的方法,但它给出了错误,它不是可观察的方法

I don't know what you wanted to do with switchMap but if you want to initialize an Observable chain you can use startWith() operator. 我不知道您想对switchMap做什么,但是如果要初始化Observable链,则可以使用startWith()运算符。

this.searchField.valueChanges
    .debounceTime(400)
    .startWith(true)

Or if you want to always pass only true value you can use mapTo : 或者,如果您只想始终传递真实值,则可以使用mapTo

this.searchField.valueChanges
    .debounceTime(400)
    .mapTo(true)

Use do operator 使用do运算符

Perform a side effect for every emission on the source Observable, but return an Observable that is identical to the source. 对源Observable上的每个发射执行副作用,但返回与源相同的Observable。

http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-do http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-do

 this.searchField.valueChanges
  .debounceTime(400)
  .distinctUntilChanged()
  .switchMap(term => this.data(term))
  .do(x => this.variable = true)
  .subscribe((result) => {
    this.variable = false;

  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM