无法在控制器中获取数据

时间:2014-03-30 13:21:33

标签: javascript jquery angularjs

嗨,我能够获得控制器中数据的警报。但它的警报两次。并显示$ rootScope:inprog错误..请帮帮我

app.factory('orderhistoryfactory', function( $http){ 
return {
    orderhistorydata:  function orderdata(){
        return $http({ url: 'order-history.json', method: 'GET'}) 
        }
    }
 });

app.controller('orderhistorycontroller', function($scope, orderhistoryfactory){
$scope.orderhistory = [];
   orderhistoryfactory.orderhistorydata().success(function(data){
 $scope.orderhistory = data;
alert(data)

   });
});

但当我使用下面的硬编码时,它的工作正常。

app.factory('orderhistoryfactory', function($http) {
  return {
    orderhistorydata: function() {
      return [
      {"orderid": "1","receipt": "121abc","deliveredto": "becca","status": "1","date": "1", },
      {"orderid": "1","receipt": "121abc","deliveredto": "becca","status": "1","date": "1", },
      {"orderid": "1","receipt": "121abc","deliveredto": "becca","status": "1","date": "1", }];
    },
  };
});

app.controller('orderhistorycontroller', function($scope, orderhistoryfactory){
 $scope.orderhistory= orderhistoryfactory.orderhistorydata();
});

1 个答案:

答案 0 :(得分:0)

我不确定出了什么问题,但你可以尝试一下,看看它是否有效。

将您的工厂服务更改为:

app.factory('orderhistoryfactory', function($http) {
    return {
        orderhistorydata: function() {
            return $http({ url: 'order-history.json', method: 'GET'})
                .success(function(data) {
                    return data;
                })
                .error(function(errorMessage) {
                    console.log(errorMessage);
                });
            }
    }

然后在工厂调用success后,在控制器中更改then。多数民众赞成我如何总是格式化我的工厂电话,我从来没有得到任何重复的回复。

希望这有帮助!