我有一个简单的视图,其中显示了一堆JSON数据。我面临的问题是,当我尝试直接从工厂内显示数据时,会显示数据,但是当通过$http.get
请求访问时,不会显示相同的数据!正在获取数据,但在获取请求期间JSON格式正在发生变化。
我的控制器代码如下:
angular.module('ngCribs')
.controller('cribsController', function($scope, cribsFactory) {
$scope.cribs;
cribsFactory.getCribs().then(function(data) {
console.log(data);
$scope.cribs = data;
}, function(data) {
console.log(data);
});
});
我的工厂代码:
angular.module('ngCribs')
.factory('cribsFactory', function($http) {
function getCribs() {
return $http.get('data.json');
}
return {
getCribs: getCribs
};
});
我创建的JSON对象:
[
{
"type": "Condo",
"price": 220000,
"address": "213 Grove Street",
"description": "Excellent place, really nice"
},
{
"type": "House",
"price": 410000,
"address": "7823 Winding Way",
"description": "Excellent place, really nice and beautiful"
},
{
"type": "Duplex",
"price": 395000,
"address": "2834 River Lane",
"description": "Great, really nice"
}
]
在控制台上输出的是什么:
这是到目前为止我的进展的一个附带链接
https://plnkr.co/edit/GTZJC2cCCzE1KfLAD5wF?p=preview
正如您在plunker中看到的那样,数据没有显示出来。请帮助并指导我在哪里犯错。
感谢。
答案 0 :(得分:1)
在cribsController.js
中,您将获得整个响应对象,将其更改为data.data
以仅访问响应数据。
cribsFactory.getCribs().then(function(data) {
console.log(data);
$scope.cribs = data.data;
}, function(data) {
console.log(data);
});
答案 1 :(得分:0)
更改<div class="well" ng-repeat="crib in cribs">
要
<div class="well" ng-repeat="crib in cribs.data">
您应该使用data.data来循环。