如何从Angular Mat-Select中获取先前值和新值? [英] How to obtain previous and new value from Angular mat-select?

查看:128
本文介绍了如何从Angular Mat-Select中获取先前值和新值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

兄弟.我使用的是6号角和敏捷材料,并且有一个字符串数组,这些字符串显示在垫选择形式"字段中. 如果用户选择一个元素,然后选择另一个,则需要跟踪以前的值是什么,新的值是什么.

Hellow. I'm using angular 6, and agular material, and I have an array of strings, which I display in a mat-select form field. If an user selects one element, and then another, I need to track what was the previous value and what is the new value.

到目前为止,我已经可以使用$ event.value获取当前值,但是还没有找到存储或获取先前值的方法.

So far, I've been able to obtain current value using $event.value, but I haven't found a way to store or obtain what the previous value was.

   <mat-select  placeholder="Choose..." (selectionChange) ="checkTitle($event.value);">
            <mat-option *ngFor="let option of Titles; trackBy: trackByFn" [value]="option.title">
              {{option.title}}
            </mat-option>
    </mat-select>

到目前为止,关于如何解决此问题,我还没有提出任何想法. 感谢您的帮助.

So far, I haven't come up with any ideas as to how solve this problem. Help is appreciated.

推荐答案

您可以通过将值推送到Subject中来处理先前值和当前值,并使用成对运算符观察此Subject.该运算符将发出流的先前值和当前值. ( https://www.learnrxjs.io/operators/combination/pairwise.html)

You can handle previous and current value by pushing the value into a Subject, and observe this Subject using the pairwise operator. This operator will emit the previous and the current value of the stream. (https://www.learnrxjs.io/operators/combination/pairwise.html)

示例:


export class YOU_COMPONENT{

  private data: Subject<any> = new Subject<any>();

  checkTitle(value){
    this.data.next(value);
  }

  observeDataChange():Observable<[]>{
     // this return an observable of an array that contains [previous, current] data
     return this.data.asObservable().pipe(pairwise());
  }

}



这篇关于如何从Angular Mat-Select中获取先前值和新值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆