rxjs

How to organize Angular data service

此生再无相见时 提交于 2019-12-12 09:28:18
问题 I have become slightly confused over Observables, Observers, Subjects, BehaviorSubjects, Services, injections, etc. I am trying to make a simple finance app. I have a transactions service that grabs the data from a REST server and then provides it to other services that can update it if they need to. I can get the list transactions component (which gets its data from getTransactions() )to update with the service, but I don't know how to get the edit component (which gets its data from "

RxJs How to set default request headers?

亡梦爱人 提交于 2019-12-12 09:19:52
问题 Not sure is there any way to set default request headers in rxjs like we do with axios js as- axios.defaults.headers.common['Authorization'] = 'c7b9392955ce63b38cf0901b7e523efbf7613001526117c79376122b7be2a9519d49c5ff5de1e217db93beae2f2033e9'; Here is my epic code where i want to set request headers - export default function epicFetchProducts(action$, store) { return action$.ofType(FETCH_PRODUCTS_REQUEST) .mergeMap(action => ajax.get(`http://localhost/products?${action.q}`) .map(response =>

Async pipe not working with Subject

China☆狼群 提交于 2019-12-12 09:08:46
问题 I have the following BehaviorSubject in a service: isAuthenticated = new BehaviorSubject<boolean>(false); And I am using it as follows in a component: authenticated: Observable<boolean>; constructor(private accountService: AccountService) { } ngOnInit() { this.authenticated = this.accountService.isAuthenticated.asObservable(); } And in the template I do something like : <li class="login-button" *ngIf="!authenticated | async"> <a (click)="authenticate()">Log in</a> </li> <li *ngIf=

How to properly chain rxjs 6 observables?

醉酒当歌 提交于 2019-12-12 08:57:53
问题 Any suggestions, how this can be rewritten in more promise-chaining style?: this.apiService.sendPutRequest('/api/users/activate', usrObj).pipe( map(() => { return this.apiService.sendGetRequest('/api/users/' + this.currentUserId).pipe( map(data => { return this.setActiveUser(data).pipe( map(() => { return this.apiService.sendGetRequest('api/tasks/user/' + this.currentUserId).pipe( map(tasks => { return this.taskService.setCurrentUserTasks(tasks); }) ); }) ); }) ); }) ); 回答1: You can use

Rxjs: Chunk and delay stream?

天大地大妈咪最大 提交于 2019-12-12 08:48:55
问题 In short, trying to chunk a really large array into chunks of 10 and wait 5 seconds before emitting the next 10. Here is what I currently have Rx.Observable .from(hugeArray) .bufferCount(10) .delay(5000) //want to wait 5 secs .flatMap(e => e) // this needs to go after to flatten the array, buffer spits out arrays of entries .flatMap( (data, index) => Rx.Observable.create(observer => { // going to render stuff here observer.onNext(data) observer.onCompleted(); })) .subscribe(val => console.log

How to take first occurrence and then supress events for 2 seconds (RxJS)

给你一囗甜甜゛ 提交于 2019-12-12 08:43:38
问题 I think RxJS should perfectly fit to supress dublicate button clicks for 2 seconds. However, Im struggleing with the implementation. var $button = $('#myButton').button(); $button .toObservable("click") //.Throttle(2000) // Wouldn't fire the first event instantly :-( .Subscribe(function(){ alert('clicked'); }); I already created a jsFiddle for your convenience. You need to scroll down in this fiddle, because I just pasted Rx inside as I couldn't find a CDN. http://jsfiddle.net/cburgdorf/mUMFA

Angular 2 + rxjs: async pipe with .share() operator

泪湿孤枕 提交于 2019-12-12 08:33:06
问题 When using the async pipe on a observable that is using the .share() operator (due to expensive calculations in the backend), I stumbled upon this behaviour: data$ = (new Observable(observer => { let counter=0; observer.next(counter) window.setInterval(() => { observer.next(counter); counter++; }, 2000); })) .share(); Template: {{ (data$|async) !== null }} {{ (data$|async) !== null }} The output of the initial value is: true false The following outputs (after more than 2 seconds) are: true

Difference between new Observable(…) and Rx.Observable.create(…)?

*爱你&永不变心* 提交于 2019-12-12 08:18:11
问题 I'm updating our software substituting all promises (and other hairy junk) for observables. To make sure that I'm following best practices, I made a quick googlearch and noticed that in some cases, the suggested syntax is by instance whereas in other cases, the examples perform a call by factory. const byInstance = new Observable(_ => { ... }); const byFactory = Rx.Observable.create(_ => { ... }); I'm curious what's the actual difference. Are they precisely interchangeable? Is it an older

why ActivatedRoute in @angular\router params is observable?

青春壹個敷衍的年華 提交于 2019-12-12 07:52:44
问题 why ActivatedRoute in @angular\router params is observable ? I'm wondering why do I need to subscribe for Params ? and why params are async is there a case the componenet of the route will be created but the params still not populated ? and is there a way to just get the value without an observable ? 回答1: You can use route.routerState.params You can subscribe because when your application navigates to the same route with just another parameter value, the component is not recreated and you

Angular 2 return data from RxJs subscribe

允我心安 提交于 2019-12-12 07:09:00
问题 I've a service with an authenticate function - authenticate(username: string, password: string) { let ret; let packet = JSON.stringify({ username: username, password: password }); let headers = new Headers(); headers.append('Content-Type', 'application/json'); this.http.post('http://localhost:5000/api/authenticate/', packet, { headers: headers }) .map(res => res.json()) .subscribe( data => { // put return data into ret ret = data.token; }, err => console.log(err), () => { // this works!