我希望得到一个可观察的结果,它在每次值更改时减去默认值。
每次值更改时,它都会根据输入字段中输入的新值减去默认值。
例如,默认值为5。
我在输入字段中输入4
结果是5-4 =1,所以我希望在输入字段"Surface ha“中显示1。
我清除字段,然后输入3,结果是5-3 =2,所以我希望在输入字段"Surface ha“中显示2。
所以..。
如果我做了一个console.log,我可以清楚地看到可观测的结果,但是,我希望将可观测的结果显示在相同的输入字段中。
我只在开发控制台中得到这样的错误结果:
NaN
意见:
<form [formGroup]="credentialsForm" *ngIf="credentialsForm">
<mat-form-field class="example-full-width">
<mat-label>Surface Ha</mat-label>
<input matInput type="number" [value]="surface" placeholder="Libellé" formControlName='surface'>
</mat-form-field>
<ion-input class="form-control" placeholder="Valeur"
formControlName="0">
</ion-input>
</form>在组件内部:
this.surface = 5;
const observedValues = ['0']
.map(key => this.credentialsForm.controls[key].valueChanges.map(value => +value).startWith(0))
const resSurface = combineLatest(observedValues)
.pipe(map(([value0]) => { return this.surface - value0 }));
const res = resSurface.subscribe(val => { return val });
// These line below should display the "res" value
this.surface = res;发布于 2020-11-23 18:35:44
您需要将可观察到的值分配给this.surface,而不是订阅本身:
resSurface.subscribe(val => this.surface = val);或者,您可以摆脱subscribe调用,并使用角async管道直接绑定到可观察到的:
this.resSurface = combineLatest(observedValues)
.pipe(map(([value0, value1, value2]) => { return this.surface - (value0 + value1 + value2) }));然后在模板中:
<input matInput type="number" [value]="resSurface | async" placeholder="Libellé" formControlName='surface'>发布于 2020-11-24 09:32:26
我忘了从不同的默认值开始!
let defaultValueSurface = 5;
const observedValues = ['0']
.map(key => this.credentialsForm.controls[key].valueChanges.map(value => +value).startWith(0))
const resSurface = combineLatest(observedValues)
.pipe(map(([value0]) => { return defaultValueSurface - value0 }));
const res = resSurface.subscribe(val => { return val });
this.surface = res;https://stackoverflow.com/questions/64973856
复制相似问题