2016-12-20 6 views
2

サービスコールがバックエンドに送られ、オブジェクトを保存すると、コールは約束を返して番号を返します。呼び出し1つのサービスコールから別のサービスコールへ角をつけて返す

この

saveTcTemplate(item: ITermsConditionsTemplate): ng.IPromise<number> { 
     item.modifiedDate = new Date(); 
     return this.$http.post(this.api + '/SaveTcTemplate', item) 
      .then(this.returnData); 
    } 

    returnData = (response: any) => { 
     return response.data; 
    }; 

のように見える新しいオブジェクトを作成するときにこれは、すべてのフィールドが表示されるように呼ばれ、その後、自分の必要な値に設定で保存するために渡されています。

これは、オブジェクトが保存された後にオブジェクトをプルするために使用されるget関数です。

getTermsConditions(id: number): ng.IPromise<ITermsConditionsTemplate> { 
     return this.$http.get(this.api + '/GetTermsConditions', 
      { 
       params: { 
        id: id 
       } 
      }).then(this.returnData); 
    } 

これは、オブジェクトの初期構築、保存、取得、

   this.newTemplate.copyId = 0; 
       this.newTemplate.id = 0; 
       this.newTemplate.isLibrary = true; 
       this.newTemplate.studyFacilityScheduleId = this.studyFacilityScheduleId; 
       this.studyTermsConditionsService.saveTcTemplate(this.newTemplate) 
        .then(this.studyTermsConditionsService.getTermsConditions) 
        .then(this.setTemplateData); 

このように設定すると、私は成功した新しいアイテムを保存し、そのID(ng.IPromiseを持つことができていますパート)が私に戻って、私のGetサービスコールに渡されました。

このように設定すると、$ http.getが戻ってくるのが不明です。私は、関数を呼び出したので、私は私が似ている他のスタックオーバーフローの問題から理解してどう思うかより、それが明示的に何も渡さずに起きている私はこれをテストするために

.then(this.studyTermsConditionsService.getTermsConditions) 

を言うとき、私はまた、セーブを設定したパラメータだとこのようにしてください

   var data: any; 
       data = this.studyTermsConditionsService.saveTcTemplate(this.newTemplate); 
       this.studyTermsConditionsService.getTermsConditions(data) 
        .then(this.setTemplateData); 

それはうまくいきました。 $ http.Getが認識され、使用できましたが、この設定での問題は次のとおりです。 nig.IPromiseの非同期性のために、データはGet関数に約束された数値として送信されていません。これは、タイプ$$ promiseとして送信され、get関数のパラメータがNaNになります。

私は使用可能な値、つまり32を渡していますが、明示的にこの値を渡していないので、$ http.getは未定義です。

他の方法では、私は明示的にパラメータを渡しているので、$ http.getは認識され、使用可能ですが、明示的に呼び出されるパラメータはタイプ$$ promiseであり、型番号ではありません。 IPromise <数字>は時間によって解決されません。私はGet関数を呼び出します。

ここからどのように進むのですか?

答えて

2

私はあなたがthisに混乱していると思います。ファンクションリファレンスをパラメータとして直接渡すと、コンテキストで失われます。以下のように明示的に呼び出す必要があります。

コード

this.newTemplate.copyId = 0; 
this.newTemplate.id = 0; 
this.newTemplate.isLibrary = true; 
this.newTemplate.studyFacilityScheduleId = this.studyFacilityScheduleId; 
this.studyTermsConditionsService.saveTcTemplate((response) => this.newTemplate(response)) 
    .then((response) => this.studyTermsConditionsService.getTermsConditions(response)) 
    .then((response) => this.setTemplateData(response)); 

は、そうしないと、関数の内部で利用可能なthisコンテキストに役立つだろうarrow functionの使用にごfunctionを作ることができます。

getTermsConditions = (id: number): ng.IPromise<ITermsConditionsTemplate> => { .. } 
+0

.then Utlizing((応答)=> this.studyTermsConditionsService.getTermsConditions(応答))であるべきである

saveTcTemplate(item: ITermsConditionsTemplate): ng.IPromise<number> { .. } 

saveTcTemplate = (item: ITermsConditionsTemplate): ng.IPromise<number> => { .. } 

そして

getTermsConditions(id: number): ng.IPromise<ITermsConditionsTemplate> { .. } 

なければなりません問題を修正しました!ありがとうございました!問題を解決するのはここで何が起こっているのですか? (応答)=>関数(応答)を追加することによって、私は本質的に、応答が達成されるまで次の関数を処理しないように指示していますか? – Chris

+0

@ Wikster2014 no ..それは角に関係するものではなく、そのES6については、 '現在のコントローラ' this'(コンテキスト)が関数を実行している間に '以前の 'this'はコールバック関数の実行中に失われました.. –

+0

@ Wikster2014あなたは他の提案を試しましたか?それもうまくいくはずです –

関連する問題