0

I am currently attempting to return an observable from a service in Angular2. If you look at the following code, if I uncomment the line in the constructor, everything works fine, and the consumer of the service won't break. But when I try to remove the call from the constructor, it crashes with: Error: XHR error (404 Not Found) loading http://localhost:3847/rxjs/observable

I believe that it is trying to subscribe to the todo$ property prior to initialisation. So how can I create an initial observer that doesn't actually make the call to the get method straight away? That is, I need some sort of empty Observer that will stop the subscribe line from falling over.

How am I supposed to achieve this?

Tony

import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
import {Todo} from './todo';
import {Observable} from 'rxjs/observable'

@Injectable()
export class TodoService {

    public todos$: Observable<Todo[]>;

    constructor(private http: Http) {

        //this.todos$ = this.http.get('api/Todo').map(res => <Array<Todo>>res.json());
    }

    loadData() {
        this.todos$ = this.http.get('api/Todo').map(res => <Array<Todo>>res.json());
    }

}
tone
  • 1,374
  • 20
  • 47

1 Answers1

2
import {Observable} from 'rxjs/observable'

should be

import {Observable} from 'rxjs/Observable';
                               ^
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I don't think that's true. I changes to uppercase "O" and it red squiggley lined. – tone Apr 11 '16 at 07:12
  • Happens to me with the lowercase `observable`. See also http://stackoverflow.com/questions/34376854/delegation-eventemitter-or-observable-in-angular2/35568924. What does your `config.js` look like? – Günter Zöchbauer Apr 11 '16 at 07:14
  • 1
    Using auto import in webstorm it gives me uppercase 'O' as well.. and fails on lowercase. – Poul Kruijt Apr 11 '16 at 07:15