如何在AngularJs中等待来自其他客户端的响应?

时间:2015-12-11 15:13:05

标签: angularjs angular-promise angular-services angular-http

我有一个Rest Service女巫回归:{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:无法读取未定义的属性'then'

1 个答案:

答案 0 :(得分:1)

有两种方法可以创建此服务。第一个widgetRestServiceSingle只会在第一次实例化时进行一次$ http调用,然后继续返回相同的数据。当你调用它的get()方法时,第二个widgetRestServiceEvery将始终重新执行$ 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()已弃用,而{I}}和.then()不会隐藏事实上$ http会返回一个承诺。

  

$ http遗留承诺方法成功与错误已被弃用。请改用标准方法。如果$ httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$ http / legacy错误。