1
我的代碼存在問題,希望這裏有人能夠幫助我理解並指向正確的方向。問題在於嵌套承諾。一個特定的方法需要向服務器發出兩個Http請求 - 第一個是在第一個Http請求成功完成之後,第二個請求的結果應該在調用類中解析。Angular js Http promises in typecript
每當使用我的示例中顯示的模式中包含單個Http調用的方法時,它將按預期工作。下面是代碼:
服務1:
public class Service1 extends IService1 {
public PostData(url : string, data: any) : ng.IHttpPromise<any> {
return this.$http.Post(url, data);
}
public GetData (url : string) : ng.IHttpPromise<any> {
return this.$http.Get(url);
}
}
客服2:
public class Service2 extends IService2 {
private srv1 : IService1;
static inject = ["Service1"];
public constructor(service1){
this.srv1 = service1;
}
public GetLetters(ids : number[]) : ng.IHttpPromise<any>{
var scope = this;
return this.srv1.PostData("api/letters", ids).success(function (data){
return scope.srv1.GetData("api/lettters/" + data);
})
}
}
控制器
public class Controller1 {
private service2 : IService2;
private array : Letter[];
static inject = ['Service2']
public constructor (Service2 : IService2){
this.service2 = Service2;
}
public someFunc() : void
var scope = this;
// I have problems here when I try to retrieve the letters
this.service2.GetLetters(someIds).success((data) => {
array = data;
});
}
}
是否有不同的方法,我應該在這裏?控制器內的數據是來自GetLetters方法外部承諾的解析數據。我想要得到的內部承諾
我不好,我忘了提在Post方法之前返回。我也在控制器中使用'.then'。固定。關於你的答案,通過在這兩個函數中作出迴歸聲明,它是否會實現我的目標?控制器中解決的數據將具有字母? –