私はPropertyComponent(Parent)とFeaturesComponent(Child)を持っており、それぞれ独自のサービスを持っています。親からの応答を受信すると、選択した機能を子に送信し、子の応答を受け取ると、親からの選択された機能に従って「isChecked = true」を変更します。2つのサービスで2つのコンポーネントにHTTPリクエストをチェーンする方法
すべての機能が "isChecked = false"になるように、親応答の前に子応答が受信されることがあるという問題。
親の応答が受信されるまで、子リクエストを遅らせる必要があります。
プロパティモデル:
export class Property {
id: string;
numberOrName: string;
selectedFeatures: string[];
}
プロパティサービス:
export class PropertService{
getPropert(id: number){
this.http.get(baseUrl + id).map((response: Response) => <Property[]>response.json().data);
}
プロパティコンポーネント:
export class EditPropertyComponent implements OnInit{
@ViewChild('pf') propertyFeatures: PropertyFeaturesComponent;
editProperty: Property;
id: string;
constructor(private propertyService: PropertyService){}
ngOnInit() {
this.route.params
.map(params => params['id'])
.do(id => this.id = id)
.subscribe(id => {
this.getProperty(id);
});
}
getProperty() {
this.propertyService.getOne(this.id).subscribe(
prop => {
this.editProperty = prop;
this.selectedFeatures = prop.selectedFeatures;
this.propertyFeatures.selectedFeatures = this.selectedFeatures;
}
);
}
}
機能サービス:
export class FeatureService {
constructor(private http: Http) { }
get() {
return this.http.get(this.baseURL)
.map((response: Response) => {console.log(response.json().data); return <Feature[]>response.json().data;});
}
}
プロパティの特徴成分(子):
export class PropertyFeaturesComponent implements OnInit {
@Input() selectedFeatures: string[];
features: Feature[];
constructor(private featureService: FeatureService) { }
ngOnInit() {
this.getFeatures();
}
getFeatures() {
this.featureService.get()
.subscribe((res) => {
this.features = res;
//If the parent response received before this subscribe then selectedFeatures will contain values otherwise the next line will be useless. this.selectedFeatures is an @Input() and I send its value from parent
this.features.filter(f => this.selectedFeatures.find(s => s == f.id)).forEach(f => f.isChecked = true);
});
}
}
私は問題がクリアされたいです。
いくつかのコードを表示できますか? –
OK、私は必要なものを簡略化しようとします – Fakhry
子供が親から応答を受け取ったら、子供は他のデータを取得するためにサービスリクエストを発行しますか?子コンポーネントの 'ngOnInit()'にデータを取得するロジックを置くと、遅延の問題があるのですか? – Chybie