Reputation: 3223
I have these two set in a component.
cards$: EventEmitter<any> = new EventEmitter();
private cards: Array<Cards>;
When i run the http.get below, how do i trigger cards$.emit(this.cards);
this.cards = [];
http.get('http://myendpoint')
.map((res: Response) => res.json()).subscribe(res => {this.cards = res };
Upvotes: 1
Views: 1429
Reputation: 657338
Add a complete callback:
this.cards = [];
http.get('http://myendpoint')
.map((res: Response) => res.json())
.subscribe(
res => { this.cards = res },
error => { console.log(error) },
() => this.cards$.emit(this.cards));
This way works if you expect more than one event and want to execute code after the last event. From Http
you'll only get one event and in this case @Sasxa s approach is probably easier.
Upvotes: 0
Reputation: 41264
Just like you said cards$.emit(this.cards);
. Add this to your subscribe onNext()
callback:
http.get('http://myendpoint')
.map((res: Response) => res.json())
.subscribe(res => {
this.cards = res;
this.cards$.emit(this.cards);
};
You also should subscribe to it somewhere else to use this value, for example:
constructor() {
this.cards$.subscribe(c => console.log(c));
}
Upvotes: 4