【问题标题】:Make AngularJS service call async使 AngularJS 服务调用异步
【发布时间】:2014-06-23 08:50:18
【问题描述】:

我已经阅读了有关“promise”对象以及获取某种异步调用或等到 http 调用完成的所有方法,但我未能成功。这就是我得到的,也是我想做的:

我需要从我的服务器获取一些 json 文件,并在我的代码(js 文件)中使用来自该 json 的数据,而不仅仅是作为我的 HTML 模板的数据。

我有一个调用 json 文件的服务:

mobilityApp.service('serveiWebservices', function($resource) {       
    return {        
        getWS: function($scope) {           
            var path = 'jsonWS/context.json';
            $resource(path).get(function (data) {                                                       
                console.log(data); //data is printed fine
                $scope.returnData = data; //just to test, it doesn't work neither
                return data;            
            });
        }
    };    
});

从我的控制器我这样称呼它:

var data = serveiWebservices.getWS($scope);     
console.log(data); //undefined

关于如何使用函数返回的承诺对象并在获得请求的数据后立即执行操作的任何想法?我知道我可以设置一个“成功”函数,但我不想使用回调。

Tnanks 提前!

【问题讨论】:

    标签: json angularjs asynchronous service promise


    【解决方案1】:

    我一直在寻找有效的解决方案。谢谢@Ross。

    这也行得通,我修改了罗斯示例,并删除了第一个返回:

    mobilityApp.service('serveiWebservices', function($http) {       
        this.getWS = function() {           
            var path = 'jsonWS/context.json';
            return $http.get(path, function (response) {                                                       
                console.log(JSON.stringify(response, null, 4));
                return response.data;            
            });
        }
        this.getWS2 = function() {           
            var path = 'jsonWS2/context.json';
            return $http.get(path, function (response) {                                                       
                console.log(JSON.stringify(response, null, 4));
                return response.data;            
            });
        }
    });
    

    如果我在这项服务中获得了很多功能,我应该使用带有所有功能的 Ross 示例,还是这个?谢谢

    【讨论】:

      【解决方案2】:

      这应该可以 -

      服务:

      mobilityApp.service('serveiWebservices', function($http) {       
          return {        
              getWS: function() {           
                  var path = 'jsonWS/context.json';
                  return $http.get(path, function (response) {                                                       
                      console.log(JSON.stringify(response, null, 4));
                      return response.data;            
                  });
              }
          };    
      });
      

      控制器:

      serveiWebservices.getWS().then(function(data) {
          console.log(JSON.stringify(data, null, 4));
      });
      

      如果你想使用 $resource 这应该也可以 -

      mobilityApp.service('serveiWebservices', function($resource) {       
          return {        
              getWS: function() {           
                  var path = 'jsonWS/context.json';
                  return $resource(path).get(function (response) {                                               
                      console.log(JSON.stringify(response, null, 4));
                      return response; // might just be response, no response.data
                  });
              }
          };    
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-23
        • 2023-03-15
        • 1970-01-01
        • 2011-09-03
        • 1970-01-01
        相关资源
        最近更新 更多