【发布时间】:2016-05-15 04:02:24
【问题描述】:
我正在使用具有此方法的服务:
export class TestModelService {
public testModel: TestModel;
constructor( @Inject(Http) public http: Http) {
}
public fetchModel(uuid: string = undefined): Observable<string> {
if(!uuid) {
//return Observable of JSON.stringify(new TestModel());
}
else {
return this.http.get("http://localhost:8080/myapp/api/model/" + uuid)
.map(res => res.text());
}
}
}
在组件的构造函数中我是这样订阅的:
export class MyComponent {
testModel: TestModel;
testModelService: TestModelService;
constructor(@Inject(TestModelService) testModelService) {
this.testModelService = testModelService;
testService.fetchModel("29f4fddc-155a-4f26-9db6-5a431ecd5d44").subscribe(
data => { this.testModel = FactModel.fromJson(JSON.parse(data)); },
err => console.log(err)
);
}
}
如果一个对象来自服务器但我正在尝试创建一个可观察的对象,该对象将与给定的subscribe() 调用静态字符串一起工作(当testModelService.fetchModel() 没有收到 uuid 时会发生这种情况)所以有两种情况下的无缝处理。
【问题讨论】:
标签: typescript promise angular observable rxjs