javascript - 如何在 Angular 10 中获取下拉列表的新旧值
问题描述
我对角度非常陌生,仍处于基本阶段。
选择下拉列表时,我试图同时获取旧值和新值,但不知道该怎么做。我能够获得新价值,但不能获得旧价值。
需要将旧值和新值都传递给 Angular 函数。
body.component.html:
<div class="container">
<mat-label>Destination 1</mat-label>
<mat-select #box (selectionChange)="removeFromPlanet(box.oldValue, box.newValue)">
<mat-option *ngFor="let planet of planets" [value]="planet">
{{ planet }}
</mat-option>
</mat-select>
body.component.ts:
removeFromPlanet(old, new) {
console.log(old, new);}
但它不起作用。我在哪里做错了?
解决方案
您可以通过将值推送到主题中来处理先前值和当前值,并使用成对运算符观察此主题。此运算符将发出流的先前值和当前值。(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());
}
}
推荐阅读
- c# - NLog 变量值的编程更改未生效
- java - Spring-WS:如何发送已经封装的消息?
- c# - 如何遍历许多具有相似名称的组合框?
- angular - 转换对象列表并通过与相同的 Id 值比较转换为不同的格式
- java - Spring boot maven多模块项目:编译失败找不到符号
- typescript - 量角器 - 黄瓜:如果验证失败,则继续场景验证步骤,而不是跳过
- node.js - 如何从nodejs将一些数据插入到excel的单元格中
- django - 我的引导下拉项目不可点击(显示)
- c++ - 在 C++ 中打印 3 维数组的内容 .. 它的第 1 维来自索引,第 2 维来自另一个数组,第 3 维来自另一个数组?
- sql - 使用嵌套数组将 JSON 文件导入 SQL Server 表