admin管理员组文章数量:1327802
Consider an Angular reactive form with an input. Whenever the input changes, we want to keep its old value and display it some where. the following code does it as displayed:
@Component({
selector: 'my-app',
templateUrl: './appponent.html',
styleUrls: [ './appponent.css' ]
})
export class AppComponent {
name = 'Reactive Form';
changedValue;
oldValue;
ooldValue;
rform = new FormGroup({
inputOne: new FormControl('chang me')
});
onOneChange(event) {
this.changedValue = event.target.value;
console.log('oneChanged', this.changedValue, 'old value is', this.oldValue);
this.ooldValue = this.oldValue;
setTimeout( ()=>this.oldValue = this.changedValue, 1);
}
}
<form [formGroup]="rform">
<label>
One:
<input formControlName="inputOne" (change)="onOneChange($event)"/>
</label>
</form>
<p>
changed value: {{changedValue}}
</p>
<p>
old value: {{ooldValue}}
</p>
Consider an Angular reactive form with an input. Whenever the input changes, we want to keep its old value and display it some where. the following code does it as displayed:
@Component({
selector: 'my-app',
templateUrl: './app.ponent.html',
styleUrls: [ './app.ponent.css' ]
})
export class AppComponent {
name = 'Reactive Form';
changedValue;
oldValue;
ooldValue;
rform = new FormGroup({
inputOne: new FormControl('chang me')
});
onOneChange(event) {
this.changedValue = event.target.value;
console.log('oneChanged', this.changedValue, 'old value is', this.oldValue);
this.ooldValue = this.oldValue;
setTimeout( ()=>this.oldValue = this.changedValue, 1);
}
}
<form [formGroup]="rform">
<label>
One:
<input formControlName="inputOne" (change)="onOneChange($event)"/>
</label>
</form>
<p>
changed value: {{changedValue}}
</p>
<p>
old value: {{ooldValue}}
</p>
As you can see it has been addressed by keeping three variables in the code which is not desirable (Yes the changedValue
variable can be removed, but still two variables to keep the old value is annoying, isn't it?).
Is there any way to rewrite the code with less variables? Does Angular itself has a descent way to do that?
You can find the code here
Share Improve this question asked Nov 14, 2019 at 15:22 RezKeshRezKesh 2,9422 gold badges28 silver badges37 bronze badges 02 Answers
Reset to default 6valueChanges is an Observable so you can pipe pairwise to get the previous and next values in the subscription.
// No initial value. Will emit only after second character entered
this.form.get('inputOne')
.valueChanges
.pipe(pairwise())
.subscribe(([prev, next]: [any, any]) => ... );
// Fill buffer with initial value, and it will emit immediately on value change
this.form.get('inputOne')
.valueChanges
.pipe(startWith(null), pairwise())
.subscribe(([prev, next]: [any, any]) => ... );
this.rform
.controls["inputOne"]
.valueChanges
.subscribe(selectedValue => {
console.log('New Value: ', selectedValue); // New value
console.log('Old Value: ', this.rform.value['inputOne']); // old value
});
本文标签: javascriptAngular reactive form having the old value on (change) event callbackStack Overflow
版权声明:本文标题:javascript - Angular reactive form having the old value on (change) event callback - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742234428a2437811.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论