【问题标题】:Can't save json data to variable (or cache) with angularjs $http.get无法使用 angularjs $http.get 将 json 数据保存到变量(或缓存)
【发布时间】:2015-06-22 08:59:15
【问题描述】:

我有奇怪的 angularjs 问题。我正在尝试从 Rest Webservice 获取数据。它工作正常,但我无法将 json 数据保存到对象。我的代码如下:

services.service('customerService', [ '$http',  '$cacheFactory', function($http, $cacheFactory) {
var cache = $cacheFactory('dataCache');
var result = cache.get('user');
this.getById = function(id){
    $http.get(urlList.getCustomer + id).success(function(data, status, headers, config) {
        result = data;
        cache.put('user', result);
        console.log(data);
    }).error(function(data, status, headers, config) {
        //
    });
    return cache.get('user');
};
}]);


services.service('customerService', [ '$http', function($http) {
var result;
this.getById = function(id){
    $http.get(urlList.getCustomer + id).success(function(data, status, headers, config) {
        result = data;
        console.log(data);
    }).error(function(data, status, headers, config) {
        //
    });
    return result;
};
}]);

这两种方法都不起作用。我错过了什么吗?

PS:“console.log”方法成功打印json数据。

编辑:

经过一些编辑,我得到了这样的结果:

services.service('customerService', [ '$http', function($http) {
var result;
this.getById = function(scope, id){
    $http.get(urlList.getCustomer + id).success(function(data, status, headers, config) {
        result = data;
        console.log(data);
        scope.userData = data;
    }).error(function(data, status, headers, config) {
        //
    });
    return result;
};
}]);

在控制器中:

customerService.getById($scope, id);

有人知道将数据保存在变量中(为其他控制器兑现)并在屏幕上显示数据的更好方法吗?我想尽可能多地跳过样板代码:)。

【问题讨论】:

    标签: json angularjs web-services rest


    【解决方案1】:

    $http.get 是异步的。当cache.getreturn result被执行时,HTTP请求还没有完成。你将如何使用这些数据?在 UI 中显示?例如。尝试以下方法:

    // Some View
    <div>{{myData}}</div>
    
    // Controller
    app.controller('MyController', function ($scope, $http) {
      $http.get('yoururl').success(function (data) {
        $scope.myData = data;
      });
    });
    

    你明白了吗?

    查看如何使用 Promise。从您的服务函数中,您可以返回 $http.get 返回的承诺,您可以在控制器/指令中使用它,如下所示:

    // in your service
    this.getById = function(id){
      return $http.get(urlList.getCustomer + id).success(function (data) {
        return data;
      });
    }
    
    // in directive/controller
    customerService.getById(someId).then(function (customer) {
      $scope.customerId = customer.id;
    });
    

    【讨论】:

      【解决方案2】:

      这就是你的方法应该是什么样子的,success 是一个异步的承诺,这意味着你的return result;success 被触发之前被调用

      return {      
              getById : function(id){
                  return $http.get(urlList.getCustomer + id).success(function(data, status, headers, config) {
                      return data;
                  }).error(function(data, status, headers, config) {
                      //
                  });
              };
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-26
        • 2014-10-03
        • 1970-01-01
        • 2014-07-14
        • 2017-07-23
        • 2015-01-31
        • 2015-03-22
        相关资源
        最近更新 更多