如何从可观察到的Angular 8&#x2B中获取初始值
我有一个页面形式,其值已设置,上一个和下一个按钮元素。
在ngoninit,我得到了一个列表,其中包含3个项目,可观察到,因为初始值 - 有时我得到4个项目。
在进入下一页之前4个项目。
因此,当我单击Next Button OnNextButton()
时,我想使用初始值与当前的值进行比较,或者检查此列表是否具有任何更改(任何增量) 。
我的操作方式无法保留/存储第一个值。在下一个按钮上,我将获得更新的值,而是上一个。
我的代码:
export class PageComponent implements OnInit {
questions$: Observable<any[]>;
hasChange: boolean;
ngOnInit() {
// GETTING INITIAL VALUE
this.questions$.subscribe((quest: any[]) => {
this.hasChange = quest.length > 3 ? true : false
});
}
calculate() {
// calling a request
// at this point, my observable will update and will have 4 items.
}
onNextButton() {
if (hasChange) {
// submit()
} else {
// navigate()
}
}
}
因此,在这种情况下,初始值应该是带有3个项目的列表,但是我得到4个,它打破了我的逻辑...
我如何获得可观察的先前值并将其存储在变量中? 或者,如何检测任何更改?
我尝试了RXJS的capen> caveniorSubject
和pairwise
,但我不确定如何应用它。
谢谢你!!!
I have a page form with values already setted, previous and next button elements.
At ngOnInit, I'm getting a List with 3 items from an observable, as initial value - sometimes I get 4 items.
Before I go to the next page I have to click, necessarily, on a button that will call the function calculate()
that will make a request and my observable List will have 4 items.
So, when I click on next button onNextButton()
I would like to use the initial value to compare with the current, if they are the same, or check if this list had any changes (any incrementing).
The way that I'm doing, I'm not manage to keep/store the first value. On next button click, i'm getting the updated value, instead the previous.
My code:
export class PageComponent implements OnInit {
questions$: Observable<any[]>;
hasChange: boolean;
ngOnInit() {
// GETTING INITIAL VALUE
this.questions$.subscribe((quest: any[]) => {
this.hasChange = quest.length > 3 ? true : false
});
}
calculate() {
// calling a request
// at this point, my observable will update and will have 4 items.
}
onNextButton() {
if (hasChange) {
// submit()
} else {
// navigate()
}
}
}
So, in this scenario the initial value should be a list w/ 3 items, but i'm getting 4 and its breaking my logic...
How do I get the previous value of Observable and store it in a variable?
Or, how can I detect any changes?
I tried behavioursubject
and pairwise
from RxJS, but I'm not sure how to apply it.
Thank you!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如您所述,您必须使用reploaysubject或chativiourSubject,因为可观察到的“内存”无法检索最后一个发射值。
replaySubject和chativiourSubject是相似的,两者都在订阅上发送最后一个发射值,但是replaySubject仅在订阅和civationUrsubject上放置最后发射值的列表列表了最后一个发射值(可配置的大小)。我会使用chativoiursubject,因为要完整一些。
只需替换您使用的行为主体使用的可观察到的可观察到的,请记住,当您实例化类时,您必须在记忆大小上进行限制。例如,您可以创建一种方法,该方法返回一个行为对象,并以这样的方式存储了最后一个布尔值:
当您这样订阅时:
最后存储的值(true)会自动检索并在控制台中显示,但至少确保您至少已经发出了一个,或者您不会执行订阅,直到调用下一个方法为止。之后,该值的每个更新都会触发具有更新值的console.log。
应用于您的代码,您可以在ngoninit中创建extionUrsubject,在ngoninit中也订阅它以定义回调事件,并在必须更新列表后调用chitapiourSubject的下一个方法。
As you stated, you must use ReplaySubject or BehaviourSubject, since Observable does not have that "memory" for retrieve last emitted value.
ReplaySubject and BehaviourSubject are similar, both send last emitted value on subscription, but ReplaySubject only emits last emitted value on subscription and BehaviourSubject stores a list of last emitted values (configurable size). I'll be using BehavoiurSubject since is a bit more complete.
Just replace the Observable you were using with the BehaviourSubject, and keep in mind that you must definde that memory size when you instance the class. For example, you could create a method that returns a BehaviourSubject with last boolean stored like this:
When you subscribe like this:
the last stored value (true) automatically will be retrieved and shown in console, but ensure you at least have emitted one first, or you wont execute the subscription until one next method is called. After that, every update of the value will trigger that console.log with the updated value.
Applied to your code, you could create the BehaviourSubject in the ngOnInit, subscribe to it also in the ngOnInit to define the callback event, and call the next method of the BehaviourSubject once the list must be updated.