角度订阅在值更改之前不起作用(需要先初始化)


this.skusChangeSubscription = this.productForm.get('skus').valueChanges
.pipe(debounceTime(600)).subscribe(skusValue => {
console.log('SKUS: ', skusValue);
...
...
});

我需要代码来直接运行这个订阅方法一次,直到值发生变化。它在值更改时起作用,但在值更改之前订阅时不起作用。我需要在订阅时运行一次。

尝试使用startWith运算符。在从可观察的源发出之前,它首先发出指定为参数的项。

this.skusChangeSubscription = this.productForm.get('skus').valueChanges
.pipe(
startWith(this.productForm.get('skus').value),
debounceTime(600)
)
.subscribe(skusValue => {
console.log('SKUS: ', skusValue);
...
...
});

试试这个:

this.skusChangeSubscription = this.productForm.get('skus')
.pipe(take(1)).subscribe(skusValue => {
console.log('SKUS: ', skusValue);
...
...
});

最新更新