How to create an Observable from static data similar to http one in Angular?

后端 未结 5 1181
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 17:36

I am having a service that has this method:

export class TestModelService {

    public testModel: TestModel;

    constructor( @Inject(Http) public http: Ht         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 18:35

    Things seem to have changed since Angular 2.0.0

    import { Observable } from 'rxjs/Observable';
    import { Subscriber } from 'rxjs/Subscriber';
    // ...
    public fetchModel(uuid: string = undefined): Observable {
      if(!uuid) {
        return new Observable((subscriber: Subscriber) => subscriber.next(new TestModel())).map(o => JSON.stringify(o));
      }
      else {
        return this.http.get("http://localhost:8080/myapp/api/model/" + uuid)
                .map(res => res.text());
      }
    }
    

    The .next() function will be called on your subscriber.

提交回复
热议问题