0

Its said that we can convert anything into stream using rxjs, here i want to convert the data coming from input field into stream and later subscribe to it, there is method in angular2 for this using valueChanges event

this.input.valueChanges.subscribe( 
   (value: string) => { console.log('sku changed to: ', value); } 
); 

but when Im trying to create stream in component class like this

Observables.create().subscribe()

the create is not recognized, also how can i do this in rxjs as value changes into input field, its related to angular2 forms, also what is useful function to create stream for this

5
  • Looks like a dup of stackoverflow.com/questions/35800792/… Commented Mar 4, 2016 at 17:06
  • no i know how to create stream but problem is Observables.create giving error in the component class like its not allowed in class to create, how can i make avaliable it into component class Commented Mar 4, 2016 at 17:15
  • move it into a function Commented Mar 4, 2016 at 17:21
  • 1
    this.input.valueChanges is already an Observable<string> that you can subscribe() to in order to get the stream of changes. What is your actual question? Commented Mar 4, 2016 at 18:48
  • I find that using a Subject works great. For example: var stream = new Subject<string>(); stream.next("some changes"); Of course, you have to import Subject. The Subject is an Observable that can be subscribed to. Commented Mar 5, 2016 at 9:12

2 Answers 2

4

Angular 2 already have a FormControl which exposes a valueChanges observable that you can subscribe to.

Here is a working Plunker and the code:

The important parts are:
- [FormControl]="model" (in the input element)
- model = new FormControl() (from @angular/forms)

import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';

@Component({
    selector: 'my-app',
    template: `
      Some Value: 
      <input type="text" [formControl]="model" />
      <div>{{model.value}}</div>
      <span *ngFor="let val of values">{{val}},</span>
    `
})
export class AppComponent {
  model:FormControl = new FormControl()
  values = [];
  constructor() {
    this.model.valueChanges.subscribe(s=>this.values.push(s));
  }
}

Also, in your AppModule you need to import ReactiveFormsModule

import { ReactiveFormsModule } from '@angular/forms';
...
@NgModule({
    imports: [
        ...
        ReactiveFormsModule
    ],
    declarations: [...],
    bootstrap: [...]
})

export class AppModule { }
Sign up to request clarification or add additional context in comments.

1 Comment

link to NgFormControl is dead, Control is also not found
0

I'm not sure to understand your problem but you can create a raw stream with the Observable class and its create method like this:

var observable = Observable.create((observer) => {
  // Should be execute asynchronously
  observer.next('some event');
});

In Angular2, the valueChanges observable allows to initiate an asynchronous processing chain. You can leverage operators like flatMap to link another observable. For example to execute an HTTP request when the value of an input changes:

var observable = this.input.valueChanges
            .flatMap((val) => {
              return this.http.get('http://...').map(res => res.map();
            });

3 Comments

I know about create method, problem is in the component class where i want write code to detect change into particular field's value using observable is not allowed, its allowed outside component class
What isn't exactly allowed in your component class? Just want to correctly understand your problem...
Why is the use of Observable.create not possible in your component class? See this plunkr: plnkr.co/edit/XqHYTqL2BadyVltajVhs?p=preview.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.