1

Hi i have a simple component and a service.When user clicks on submit button i am calling a service where an async subject emits a value to all its subscribers.I have done it using subject it works fine but i want to do it using Async Subject.I have made a demo here http://plnkr.co/edit/tHjPVZ5NurJRWdCM9lbt?p=preview ...This is my Service...

export class ErrorService{
  latestError:AsyncSubject<>=new AsyncSubject();

  error(){
    this.latestError.next('form submitted');
  }
}

And this is my component class where i am subscribing the asyncsubject

export class AppComponent { 
  result:any;
  constructor(private service:ErrorService){
    this.service.latestError.subscribe(err=> this.result=err);
  }

  onSubmit() { 
    this.service.error();
  }
}

But this is throwing error...I dont know where i am doing wrong...Somebody please help me to rectify this error and show me how to use async subject and emit values to its subscribers

1 Answer 1

4

You need to include the class this way:

import {AsyncSubject} from 'rxjs/subject/AsyncSubject';

@Injectable()
export class ErrorService{
  latestError:AsyncSubject<>=new AsyncSubject();
  error(){
    this.latestError.next('form submitted');
  }
}

See this plunkr: http://plnkr.co/edit/SAl4YVXKRWpS7OeTYMrd?p=preview.

Sign up to request clarification or add additional context in comments.

4 Comments

is this import {AsyncSubject} from 'rxjs/subject/AsyncSubject'; any predefined class/import ?
But when .next() is called in my service class it emits a value.And i have subscribed the async subject in my constructor...But i am not getting the value which has been emitted by the async subject
@PardeepJain You can also import it from the rxjs/Rx module: import {AsyncSubject} from 'rxjs/Rx';.
@abhilashreddy you need to also call the complete method on the subject to receive your events. I updated my plunkr... plnkr.co/edit/SAl4YVXKRWpS7OeTYMrd?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.