【问题标题】:Angular2 search add boolean to map valuechangesAngular2搜索添加布尔值以映射值更改
【发布时间】:2017-05-12 15:49:37
【问题描述】:
this.searchField.valueChanges
      .debounceTime(400)
      .distinctUntilChanged()
      .switchMap(term => this.data(term), this.variable = true)
      .subscribe((result) => {
        this.variable = false;

      });

在上面的代码中,当调用 this.data 可观察方法时,我想让 this.variable 为真。这是我尝试过的方式,但它给出的错误是它不是可观察的方法

【问题讨论】:

    标签: javascript angular search rxjs angular2-services


    【解决方案1】:

    我不知道你想用 switchMap 做什么,但如果你想初始化一个 Observable 链,你可以使用 startWith() 运算符。

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

    或者,如果您想始终只传递真值,您可以使用mapTo

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

    【讨论】:

      【解决方案2】:

      使用do操作符

      对源 Observable 的每个发射执行副作用,但返回与源相同的 Observable。

      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;
      
        });
      

      【讨论】:

      • 这不会在调用this.data(term) observable 方法时将this.variable 的值更改为true
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      相关资源
      最近更新 更多