如何在 AngularJs 中等到来自其余客户端的响应?
How to wait till the response comes from the rest client, in AngularJs?
我有休息服务 returns : {dataRef:Array[3],status:Object}
。
当我在控制器和 运行 应用程序中注入服务时,我仍然无法从其余服务返回数据。
我从这里阅读并尝试了其他解决方案,但我仍然无法完成这项工作。请提出一些建议?!
第一次服务
.factory('widgetRestService',['$http','UrlService','$log',
function($http,UrlService,$log){
var serviceInstance = {};
serviceInstance.dataRef = [];
serviceInstance.status = {};
var contextPath = UrlService.getContextPath();
var urlHelpRef = contextPath + "/rest/widgets/getListInfoDashboards";
var promiseData = $http({method: 'GET', url: urlHelpRef}).
success(function (data, status) {
$log.debug("widgetServiceInfo", data);
serviceInstance.status = status;
serviceInstance.dataRef = data;
}).
error(function (data, status) {
serviceInstance.data = data || "Request failed";
serviceInstance.status = status;
});
promiseData.$promise.then(function success(data,status) {
serviceInstance.status = status;
serviceInstance.dataRef = data;
});
return serviceInstance;
}]);
控制器
$log.debug('widgetRestService',widgetRestService);
结果
TypeError: Cannot read property 'then' of undefined
有两种方法可以创建此服务。第一个 widgetRestServiceSingle
只会在第一次实例化时调用 $http 一次,然后继续返回相同的数据。第二个 widgetRestServiceEvery
将始终在您调用其 get() 方法时重新执行 $http 调用。
单值(第一次实例化)源码:
.factory('widgetRestServiceSingle', ['$http', 'UrlService', '$log', function($http, UrlService, $log) {
var contextPath = UrlService.getContextPath();
var urlHelpRef = contextPath + "/rest/widgets/getListInfoDashboards";
return $http({method: 'GET', url: urlHelpRef})
.then(function (response) {
return {dataRef: response.data, status: response.status};
})
.catch(function (response) {
return {dataRef: response.data || 'Request failed', status: response.status};
});
}]);
.factory('defaultWidgets', ['widgetRestServiceSingle', '$log', 'myServicevld', function(widgetRestServiceSingle, $log, myServicevld) {
return widgetRestServiceSingle.then(function(data) {
$log.debug('hahiha',data);
});
}]);
和多个值(每个 get()
执行一个)源代码:
.factory('widgetRestServiceEvery', ['$http', 'UrlService', '$log', function($http, UrlService, $log) {
return {
get: function get() {
var contextPath = UrlService.getContextPath();
var urlHelpRef = contextPath + "/rest/widgets/getListInfoDashboards";
return $http({method: 'GET', url: urlHelpRef})
.then(function (response) {
return {dataRef: response.data, status: response.status};
})
.catch(function (response) {
return {dataRef: response.data || 'Request failed', status: response.status};
});
}
};
}]);
.factory('defaultWidgets', ['widgetRestServiceEvery', '$log', 'myServicevld', function(widgetRestServiceEvery, $log, myServicevld) {
return widgetRestServiceEvery.get().then(function(data) {
$log.debug('hahiha',data);
});
}]);
有关 Promises
的更多信息,this 是一个很好的起点。
另请注意,根据 the official Angular $http documentation,.success()
和 .error()
已被弃用,取而代之的是不隐藏的 .then()
和 .catch()
消除 $http returns 承诺的事实。
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.
我有休息服务 returns : {dataRef:Array[3],status:Object}
。
当我在控制器和 运行 应用程序中注入服务时,我仍然无法从其余服务返回数据。
我从这里阅读并尝试了其他解决方案,但我仍然无法完成这项工作。请提出一些建议?!
第一次服务
.factory('widgetRestService',['$http','UrlService','$log',
function($http,UrlService,$log){
var serviceInstance = {};
serviceInstance.dataRef = [];
serviceInstance.status = {};
var contextPath = UrlService.getContextPath();
var urlHelpRef = contextPath + "/rest/widgets/getListInfoDashboards";
var promiseData = $http({method: 'GET', url: urlHelpRef}).
success(function (data, status) {
$log.debug("widgetServiceInfo", data);
serviceInstance.status = status;
serviceInstance.dataRef = data;
}).
error(function (data, status) {
serviceInstance.data = data || "Request failed";
serviceInstance.status = status;
});
promiseData.$promise.then(function success(data,status) {
serviceInstance.status = status;
serviceInstance.dataRef = data;
});
return serviceInstance;
}]);
控制器
$log.debug('widgetRestService',widgetRestService);
结果
TypeError: Cannot read property 'then' of undefined
有两种方法可以创建此服务。第一个 widgetRestServiceSingle
只会在第一次实例化时调用 $http 一次,然后继续返回相同的数据。第二个 widgetRestServiceEvery
将始终在您调用其 get() 方法时重新执行 $http 调用。
单值(第一次实例化)源码:
.factory('widgetRestServiceSingle', ['$http', 'UrlService', '$log', function($http, UrlService, $log) {
var contextPath = UrlService.getContextPath();
var urlHelpRef = contextPath + "/rest/widgets/getListInfoDashboards";
return $http({method: 'GET', url: urlHelpRef})
.then(function (response) {
return {dataRef: response.data, status: response.status};
})
.catch(function (response) {
return {dataRef: response.data || 'Request failed', status: response.status};
});
}]);
.factory('defaultWidgets', ['widgetRestServiceSingle', '$log', 'myServicevld', function(widgetRestServiceSingle, $log, myServicevld) {
return widgetRestServiceSingle.then(function(data) {
$log.debug('hahiha',data);
});
}]);
和多个值(每个 get()
执行一个)源代码:
.factory('widgetRestServiceEvery', ['$http', 'UrlService', '$log', function($http, UrlService, $log) {
return {
get: function get() {
var contextPath = UrlService.getContextPath();
var urlHelpRef = contextPath + "/rest/widgets/getListInfoDashboards";
return $http({method: 'GET', url: urlHelpRef})
.then(function (response) {
return {dataRef: response.data, status: response.status};
})
.catch(function (response) {
return {dataRef: response.data || 'Request failed', status: response.status};
});
}
};
}]);
.factory('defaultWidgets', ['widgetRestServiceEvery', '$log', 'myServicevld', function(widgetRestServiceEvery, $log, myServicevld) {
return widgetRestServiceEvery.get().then(function(data) {
$log.debug('hahiha',data);
});
}]);
有关 Promises
的更多信息,this 是一个很好的起点。
另请注意,根据 the official Angular $http documentation,.success()
和 .error()
已被弃用,取而代之的是不隐藏的 .then()
和 .catch()
消除 $http returns 承诺的事实。
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.