あなたの問題の解決方法をご存知ですか? 私はここのように観測可能にカプセル化サブスクライブの柔軟なシーケンス必要があります。一部のシーケンシャルサブスクリプションでAngular2カスタムオブザーバブル
onSubmit() {
// Validations and others
...
if(this.isNew) {
observable = this.create();
} else {
observable = this.update();
}
return observable.subscribe(success => {
if(success == true) {
let subscription = this.saveData().subscribe(successFinished => {
// And here is the problem, because this var doesnt have the correct response
if(successFinished === true) {
this.alertService.success('ALERT.success_saved', {value: 'ALERT.success_edit_user', param: {user: this.user.username}});
}
});
subscription.unsubscribe();
}
});
メイン:私は私の提出方法に、この「レスポンス・コレクション」の結果と呼ばれる最後に
saveData() {
return new Observable((observer) => {
let success = true;
if(example === true) {
this.saveCallbarrings().subscribe((response) => {
// this response was ignored from angular
success = (response === true);
});
}
if(example2 === true) {
this.saveCallbarrings().subscribe((response) => {
// this response was ignored from angular too
success = (response === true);
});
}
// and so on ... in the end I need a result of all responses
observer.next(success);
});
}
を問題は、 "成功" varが最初のコードブロックに登録されるまで、角度が待たないということです。 なぜ私にとってより良い解決策はありますか?
ObservableまたはPromiseの完了を待つことはできません。それを購読して、イベントが完了したときに通知を受けることができます。私はあなたがそこに変更を加えることができると感じるようにsaveExtendedDataサービスメソッドを追加してください? –
申し訳ありませんが、私は2番目のコードブロックに間違いがあります。メソッド "this.saveExtendedData()"は "this.saveData()"でなければなりません。 – Joeker
成功の変数に値を代入するのではなく、データを保存するメソッド –