Bu yönteme sahip bir hizmetim var:
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());
}
}
}
bileşenin kurucusunda şu şekilde abone oluyorum:
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)
);
}
}
Bu, bir nesne sunucudan gelirse çalışır, ancak ben subscribe()statik bir dizge için verilen çağrı ile çalışacak bir gözlemlenebilir oluşturmaya çalışıyorum (bu testModelService.fetchModel(), bir uuid almadığında gerçekleşir ), böylece her iki durumda da sorunsuz bir işlem vardır.