observable

async/await in Angular `ngOnInit`

巧了我就是萌 提交于 2020-06-08 03:16:38
问题 I’m currently evaluating the pros ‘n’ cons of replacing Angular’s resp. RxJS’ Observable with plain Promise so that I can use async and await and get a more intuitive code style. One of our typical scenarios: Load some data within ngOnInit . Using Observables , we do: ngOnInit () { this.service.getData().subscribe(data => { this.data = this.modifyMyData(data); }); } When I return a Promise from getData() instead, and use async and await , it becomes: async ngOnInit () { const data = await

Returning 2 different values using using a mock service while unit testing in Angular

不羁的心 提交于 2020-06-06 08:28:25
问题 Summary of the issue : How to call multiple values using the same service inside the same test spec and check to see if it works exactly as in the component? I am using Angular7+. Let there be a component ( say A ) with a service injected into it. Suppose , there is a getNumber function in the same service with 2 parameters namely ( "Key" , "Value" ) where "Key" can only be either num1 or num2 and "Value" can be any integer . This function returns an object in this format : { "Key" : num1 or

Merging unknown number of observables with RxJS

爷,独闯天下 提交于 2020-05-29 03:51:07
问题 I am currently triing to create a small project to demo Reactive Programming with RxJS. The goal is to show my colleagues that this thing is out there, and it is worth looking into. I'm not experienced with the framework, so that makes things complicated. I am triing to expand another demo of mine to utilize RxJS. It is not a very complicated demo, basically I can add any number of small forms, wich result in a number calculated by a small formula, and there is a button, wich sums up the

how to change LiveData observable query parameter with room database in android?

给你一囗甜甜゛ 提交于 2020-05-27 05:41:28
问题 I am using live data with room database and my activity observes live data provided from room database. @Query("SELECT * FROM BUS WHERE BUS_CATEGORY = :busCategory") LiveData<List<Bus>> getLiveBuses( String busCategory); ViewModels gets LiveData via Dao(Data Access Object) and activity observes this live data. Now it works fine. But when busCategory changes i can't modify this live data to get buses for newly selected busCategory. So how can i observe this same liveData where query parameters

Angular 2+ wait for subscribe to finish to update/access variable

余生长醉 提交于 2020-05-26 10:39:07
问题 Hello I am having an issue with my variables being undefined. I am certain this is because the observable hasnt finished. Here is the part of my code in my .ts file that is causing the issue: (I'm placing the minimum code required to understand the issue, if I need to provide more code and context let me know. Also myFunction gets called from a click event in the html.) export class myClass { myVariable: any; myFunction() { this.myService.getApi().subscribe(data => { this.myVariable = data; }

RxJS observable which emits both previous and current value starting from first emission

故事扮演 提交于 2020-05-25 06:06:33
问题 I have a BehaviorSubject which emits JavaScript objects periodically. I want to construct another observable which will emit both previous and current values of the underlying observable in order to compare two objects and determine the delta. The pairwise() or bufferCount(2, 1) operators are looking like a good fit, but they start emitting only after buffer is filled, but I require this observable to start emitting from the first event of the underlying observable. subject

RxJS observable which emits both previous and current value starting from first emission

徘徊边缘 提交于 2020-05-25 06:05:23
问题 I have a BehaviorSubject which emits JavaScript objects periodically. I want to construct another observable which will emit both previous and current values of the underlying observable in order to compare two objects and determine the delta. The pairwise() or bufferCount(2, 1) operators are looking like a good fit, but they start emitting only after buffer is filled, but I require this observable to start emitting from the first event of the underlying observable. subject

unsubscribe is not a function on an observable

筅森魡賤 提交于 2020-05-25 06:03:28
问题 I'm making a drag and drop application and I have created an observable to the mouse position, that repositions my drag-object . mouseMove$: any; constructor(){ this.mouseMove$ = Observable.fromEvent(document, 'mousemove') .do((mouseevent: MouseEvent) => { if (document.getElementById("drag-object")){ document.getElementById("drag-object").style.left = (mouseevent.clientX) + 'px'; document.getElementById("drag-object").style.top = (mouseevent.clientY) + 'px'; } }); With this implementation I

Get current value from Observable without subscribing (just want value one time)

末鹿安然 提交于 2020-05-24 18:06:30
问题 The title says it all really. How can I get the current value from an Observable without subscribing to it? I just want the current value one time and not new values as they are coming in... 回答1: You need to use BehaviorSubject, BehaviorSubject is similar to ReplaySubject except it only remembers the last publication. BehaviorSubject also requires you to provide it a default value of T. This means that all subscribers will receive a value immediately (unless it is already completed). It will

How to implement ngOnDestroy() correctly in Angular?

纵然是瞬间 提交于 2020-05-23 09:12:10
问题 I have a child component with a timer, every 2 second I send api call to the server. I need to do this call as long as the user is on the page even if he/she going to a wedding and leave the page (the parent compoent window) open. Here is some code from my component: this.myTimer = Observable.timer(1, 2000); this.myTimer .mergeMapTo(this.myService.doSomeWork(this.myId)) .subscribe((data) => { this.myData = data; }, (errRes)=>{ console.log(errRes); }); And here is the destroy method: