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

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

问题描述

你好.我正在使用 angular 6 和 angular 材料,并且我有一个字符串数组,我将其显示在 mat-select 表单字段中.如果用户选择一个元素,然后选择另一个元素,我需要跟踪什么是以前的值,什么是新值.

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天全站免登陆