尝试从json angular获取数据时出错

时间:2017-05-13 15:31:45

标签: angularjs json

我试图从工厂获取数据json。我从json什么都没有,我试着看看我的控制台,我得到了这个

angular.min.js:123 Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"https://api.myjson.com/bins/d5nyl","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}

请告诉我这种错误

experience.html

  <tr ng-repeat="experience in experiences" class="">
      <td><p>{{experience.no}}</p></td>
      <td><p>{{experience.name}}</p></td>
    </tr>

(控制器)

angular.module('app').controller('experienceCtrl',['$scope','Experiences',function($scope,Experiences){
$scope.experiences= Experiences.get();
}]);

(服务)experience.js

angular.module('app').factory('Experiences',['$http', function($http){
      return{
        get: function(){
          return $http.get('https://api.myjson.com/bins/d5nyl').then(function(response){
            return response.data;
          });
        }
      };
    }])

1 个答案:

答案 0 :(得分:2)

在您的代码中,$scope.experiences是promise对象,return response.data;没有返回任何内容。您需要使用控制器中的promise对象来分配数据。

angular.module('app')
.controller('experienceCtrl',
    ['$scope','Experiences',function($scope,Experiences){

    Experiences.get()
    .then(function(response) { $scope.experiences = response.data; });
}]);

angular.module('app').factory('Experiences',['$http', function($http){
    return {
        get: function(){
          return $http.get('https://api.myjson.com/bins/d5nyl');
        }
    }
}]);