1

I'm new to Angular 2. I have an observable object, called employees. How to extract the Employee array from that Observable?
(let's say, because I need to perform operation on particular index) :

employees:Observable<Employees[]>;

And I'm intended to be

empArr:Employee[];
empArr = employees.somethingToConvertToArray(); // this is what I intended

I've tried with .subscribe or .map, but I think I'm going to wrong direction.

imeluntuk
  • 385
  • 1
  • 5
  • 15

1 Answers1

0

That's not how it works. To get data from an observable, you subscribe to it

employees.subscribe(val => this.empArray = val);
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thank you for your answer. I tried that and put log inside arrow function and it works. However this.empArray back to undefined outside that statement. – imeluntuk Apr 22 '17 at 16:34
  • That's expected behavior. You have to move all the code inside the callback. You can use `val => { ... }` to be able to add multiple statements. `val => ...` is a function passed to `subscribe(...)` and subscribe calls this function whenever a new value becomes available. Code outside of this function is executed before a value becomes available. – Günter Zöchbauer Apr 22 '17 at 16:36
  • 2
    Just like Günther said, as addition I thought I'd just throw this one out there, which explains this behavior a bit :) http://stackoverflow.com/a/43055707/6294072 – AT82 Apr 22 '17 at 17:20
  • Why it does not work for me: `this.newEducationYearModel = new Observable(); this.newEducationYearModel.subscribe(val => console.log(val));` –  Aug 17 '17 at 14:36
  • I get an error: `Cannot read property 'subscribe' of undefined` –  Aug 17 '17 at 14:38
  • @user3573738 You can use a `Subject` instead https://plnkr.co/edit/nWxSFqhoCWVmbj9IwkDM?p=preview. Check also https://angular-2-training-book.rangle.io/handout/observables/using_observables.html for an `Observable` example. – Günter Zöchbauer Aug 17 '17 at 14:44
  • What is Subject, instead Subject I have own custom model: CreateEducationYear:{id: 1, name: "ok"} on which I need subscribe. –  Aug 17 '17 at 14:46
  • `Subject` is a convenient way to create observables. You can just call `this.mySubject.next(someEventValue);` to emit new values. – Günter Zöchbauer Aug 17 '17 at 14:48
  • Can you share extended sample please? With custom complicated model? –  Aug 17 '17 at 14:50
  • I don't know what you mean with custom complicated model. I don't see how a model would be involved here. Perhaps it's better to create a new question with the code that demonstrates what you try to accomplish. – Günter Zöchbauer Aug 17 '17 at 14:55
  • This is my full code: `public newEducationYearModel: Observable; this.newEducationYearModel = new Observable(); public test(): Observable { return this.newEducationYearModel; } And calling: ngOnInit() { this.test().subscribe(data => { console.log(data); }); }` –  Aug 17 '17 at 14:55
  • Sorry, I can't make sense of that. I don't see why just using a `Subject` instead of `Observable` wouldn't work for that. Have you even tried? – Günter Zöchbauer Aug 17 '17 at 14:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152174/discussion-between-user3573738-and-gunter-zochbauer). –  Aug 17 '17 at 15:07