Javascript Angular和TypeScript:我应该如何处理这个问题
我有一个关于如何处理角度异步调用的问题 我正在从控制器调用服务中的以下两个方法,以获取一个对象,即“categoryInfo” 如何让以下方法返回categoryInfo并正确打印 调用程序(在控制器中)调用这两个方法↓强>Javascript Angular和TypeScript:我应该如何处理这个问题,javascript,angularjs,asynchronous,typescript,Javascript,Angularjs,Asynchronous,Typescript,我有一个关于如何处理角度异步调用的问题 我正在从控制器调用服务中的以下两个方法,以获取一个对象,即“categoryInfo” 如何让以下方法返回categoryInfo并正确打印 调用程序(在控制器中)调用这两个方法↓ console.log(this.service.getLargeCategoryList()); ←※Prints out as "undefined" public getLargeCategoryList(): any { console.log
console.log(this.service.getLargeCategoryList()); ←※Prints out as "undefined"
public getLargeCategoryList(): any {
console.log('Inside getLargeCategoryList');
this.getCategoryInfoList('', ServiceUrls.URL_FOR_TOP_CATEGORY)
.then((data) => {
var categoryInfo: ILCategoryInfoRes[] = data.categoryInfo;
return categoryInfo; ←※Appears to be skipped
}, function (data) {
console.log(data);
alert('Error');
return null;
});
console.log('Coming out of getLargeCategoryList');
}
private getCategoryInfoList(parameter: any, serviceUrl: string): ng.IPromise<any> {
var def = this.qService.defer();
this.httpService({
method: 'POST',
url: serviceUrl,
data: parameter
}).success(function (data: any, status, headers, config) {
//Success
var categoryInfo: ILCategoryInfoRes[];
var statusInfo: IStatus[];
//categoryInfo = data.categoryInfo;
statusInfo = data.status;
if (statusInfo[0].statusCode == '000') {
def.resolve(data);
} else {
def.resolve(statusInfo);
}
}).error(function (data, status, headers, config){
//Error
def.reject("Failed");
});
return def.promise;
}
方法1↓强>
console.log(this.service.getLargeCategoryList()); ←※Prints out as "undefined"
public getLargeCategoryList(): any {
console.log('Inside getLargeCategoryList');
this.getCategoryInfoList('', ServiceUrls.URL_FOR_TOP_CATEGORY)
.then((data) => {
var categoryInfo: ILCategoryInfoRes[] = data.categoryInfo;
return categoryInfo; ←※Appears to be skipped
}, function (data) {
console.log(data);
alert('Error');
return null;
});
console.log('Coming out of getLargeCategoryList');
}
private getCategoryInfoList(parameter: any, serviceUrl: string): ng.IPromise<any> {
var def = this.qService.defer();
this.httpService({
method: 'POST',
url: serviceUrl,
data: parameter
}).success(function (data: any, status, headers, config) {
//Success
var categoryInfo: ILCategoryInfoRes[];
var statusInfo: IStatus[];
//categoryInfo = data.categoryInfo;
statusInfo = data.status;
if (statusInfo[0].statusCode == '000') {
def.resolve(data);
} else {
def.resolve(statusInfo);
}
}).error(function (data, status, headers, config){
//Error
def.reject("Failed");
});
return def.promise;
}
方法2↓强>
console.log(this.service.getLargeCategoryList()); ←※Prints out as "undefined"
public getLargeCategoryList(): any {
console.log('Inside getLargeCategoryList');
this.getCategoryInfoList('', ServiceUrls.URL_FOR_TOP_CATEGORY)
.then((data) => {
var categoryInfo: ILCategoryInfoRes[] = data.categoryInfo;
return categoryInfo; ←※Appears to be skipped
}, function (data) {
console.log(data);
alert('Error');
return null;
});
console.log('Coming out of getLargeCategoryList');
}
private getCategoryInfoList(parameter: any, serviceUrl: string): ng.IPromise<any> {
var def = this.qService.defer();
this.httpService({
method: 'POST',
url: serviceUrl,
data: parameter
}).success(function (data: any, status, headers, config) {
//Success
var categoryInfo: ILCategoryInfoRes[];
var statusInfo: IStatus[];
//categoryInfo = data.categoryInfo;
statusInfo = data.status;
if (statusInfo[0].statusCode == '000') {
def.resolve(data);
} else {
def.resolve(statusInfo);
}
}).error(function (data, status, headers, config){
//Error
def.reject("Failed");
});
return def.promise;
}
private getCategoryFolist(参数:any,serviceUrl:string):ng.IPromise{
var def=this.qService.defer();
这个.httpService({
方法:“POST”,
url:serviceUrl,
数据:参数
}).success(函数(数据:任意、状态、标题、配置){
//成功
var categoryInfo:ILCategoryInfoRes[];
var statusInfo:IStatus[];
//categoryInfo=data.categoryInfo;
statusInfo=data.status;
如果(状态信息[0]。状态代码==“000”){
解析(数据);
}否则{
定义解析(状态信息);
}
}).error(函数(数据、状态、标题、配置){
//错误
定义拒绝(“失败”);
});
回报承诺;
}
您需要从该函数中返回承诺:
public getLargeCategoryList(): any {
console.log('Inside getLargeCategoryList');
return this.getCategoryInfoList('', ServiceUrls.URL_FOR_TOP_CATEGORY)
.then((data) => {
var categoryInfo: ILCategoryInfoRes[] = data.categoryInfo;
return categoryInfo; ←※Appears to be skipped
}, function (data) {
console.log(data);
alert('Error');
return null;
});
console.log('Coming out of getLargeCategoryList');
}
并像返回承诺一样处理它,而不是返回值,因为它是一个异步操作:
this.service.getLargeCategoryList()
.then((data) => {
console.log(data);
}):
函数
getLargeCategoryList
不返回任何内容。返回categoryInfo
只是从promise success处理程序返回,而不是从getLargeCategoryList
如何实现这一点?我添加了一个答案,解释了我所说的内容