Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emit a single value

I would like to emit a single value.

According to this post, the following should be valid:

var requestStream = Rx.Observable.just('https://api.github.com/users'); 

However, this does not work. just is not defined.

Has it been deprecated? Is there something that I can use instead?

like image 935
pixelbits Avatar asked Jan 20 '16 07:01

pixelbits


People also ask

What is first () in angular?

The first() operator takes an optional predicate function and emits an error notification when no value matched when the source completed.

What is first in RxJS?

The RxJS first() operator is generally used when you are only interested in the first item emitted by the source observable or the first item that meets some criteria. In this case, you can use this operator to filter the Observable.

What is of operator in RxJS?

The of Operator is a creation Operator. Creation Operators are functions that create an Observable stream from a source. The of Operator will create an Observable that emits a variable amount of values in sequence, followed by a Completion notification.

What is angular Observable pipe?

The pipe method of the Angular Observable is used to chain multiple operators together. We can use the pipe as a standalone method, which helps us to reuse it at multiple places or as an instance method. In this tutorial, we will take a look at the pipe and learn how to use it in an Angular Application.


2 Answers

A lot of the stuff is tucked away in: import 'rxjs/Rx'; You might try Observable.of for a single value.

This works for me (plunker):

import {Component} from 'angular2/core'; import {Observable} from 'rxjs/Observable'; import 'rxjs/Rx';  @Component({   selector: 'my-app',   providers: [],   template: `     <div>       <h2>Hello {{test | async}}</h2>          </div>   `,   directives: [] }) export class App {     test: Observable<any> = Observable.of("I'm an observable");     constructor() {   } } 
like image 181
Mark Avatar answered Sep 20 '22 03:09

Mark


.just(value) is from RxJS v4 while angular2 is using the v5. It has gone through an extensive rewrite, and it is still in beta so operators are missing/changed names in v5.

.of is just fine, as MarkM mentioned. I just wanted to add that you can also use .from which works if you have just one value, or an array of values. To reuse the example from your accepted answer, you would use something like (plunkr):

//our root app component import {Component} from 'angular2/core'; import {Observable} from 'rxjs/Observable'; import 'rxjs/Rx';  @Component({   selector: 'my-app',   providers: [],   template: `     <div>       <h2>Hello {{test | async}}</h2>      </div>   `,   directives: [] }) export class App {     test: Observable<any> = Observable.from(["I'm an observable"]);     constructor() {   } } 
like image 23
user3743222 Avatar answered Sep 22 '22 03:09

user3743222