1
最近、私はangular2に苦労しています。現在、非同期HTTPローディングに関連するいくつかの問題に取り組んでいます。ここで角度2 - 観測可能で非同期のHTTPローディング
は、私が得たものである:プロパティが取得されたデータで埋めなければならないことのREST API
- 1サービス・コンポーネント(MyCustomHttpService)ビューにデータをプッシュするためにクラスのゲッターを呼び出し
- 1ビューコンポーネント(MyCmp)Iは、Tを希望何
O操作を行います。私は"DataProvider.getDataProviderSpecification()"
- を呼び出すと
は、私はそれはそれ以外の場合はまだ
- ロードされていない場合、私はデータを返すすでに
このロジックは、DataProviderクラスにある必要があり、カスタムhttpサービスまたはビューコンポーネントではありません。
本当に汚い回避策が見つかりました。私はそれが本当に悪いことを知っているので、私はそのコードを改善する方法についてアドバイスを求めています。
ご協力いただきありがとうございます。
はそれはそのように見えます(コードは掃除):
/** CLASS **/
@Component()
export class DataProvider {
private treestructure: any = null;
constructor(private httpService: MyCustomHttpService) {}
public getDataProviderSpecification() {
if(this.treestructure == null) {
return Observable.create(observer => {
// http service to get REST data
this.httpService.getDocumentDataProviderTree(this.documentID)
.subscribe((tree)=> {
this.treestructure= tree;
observer.next(this.treestructure);
observer.complete();
});
});
} else {
return Observable.create(observer => {
observer.next(this.treestructure);
observer.complete();
});
}
}
...
}
/** VIEW COMPONENT **/
@Component({
selector: 'my-cmp',
template: '<tree-cmp [structure]="tree"></tree-cmp>',
inputs: ['dataprovider'],
directives: [TreeComponent]
})
export class MyCmp {
@Input() dataprovider: DataProvider;
tree: any;
showDetails(id) {
this.dataprovider.getDataProviderSpecification().subscribe((treestructure) => {
this.tree = treestructure;
});
}
}
ギュンターねえ!私はあなたが誤植をしていると思う/あなたはあなたのスニペットで何かを逃した。 'this.observable'は決して設定されません...私は' this.observable = this.httpService.getDocumentDataProviderTree ... 'のようなものです。 –
右コピーの貼り付けエラー。ヒントをありがとう。 –
ちょうどそれを試して、それは私が必要なものを正確に行います。ありがとう! –