我无法在视图中显示数据。我在results.data上看到了ajax加载的结果,但是我无法显示结果。
JSON数据是 -
[{
"locationId": 14,
"name": "coin"
},
{
"locationId": 11,
"name": "coop"
},
{
"locationId": 5,
"name": "dxd"
},
{
"locationId": 15,
"name": "dxd2"
},
{
"locationId": 1017,
"name": "inka"
},
{
"locationId": 16,
"name": "test-pinco"
},
{
"locationId": 13,
"name": "upimuim"
}]
查看
<div class="row">
<div class="col-md-2">
</div>
<div class="col-md-8">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>LocationId</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="order in orders">
<td>
{{ locationId }}
</td>
<td>
{{ name }}
</td>
</tr>
</tbody>
</table>
</div>
<div class="col-md-2">
</div>
</div>
控制器
'use strict';
app.controller('locationsController', ['$scope', 'locationsService', function ($scope, locationsService) {
$scope.locations = [];
locationsService.getLocations().then(function (results) {
$scope.locations = results.data;
}, function (error) {
//alert(error.data.message);
});
}]);
服务
'use strict';
app.factory('locationsService', ['$http', function ($http) {
var serviceBase = 'http://localhost:4014/';
var locationsServiceFactory = {};
var _getLocations = function () {
return $http.get(serviceBase + 'api/locations').then(function (results) {
return results;
});
};
locationsServiceFactory.getLocations = _getLocations;
return locationsServiceFactory;
}]);
答案 0 :(得分:1)
在视图中尝试此操作 -
将$ scope.locations替换为$ scope.orders,然后使用order.locationID和order.name。
答案 1 :(得分:1)
您的服务应该返回承诺:
app.factory('locationsService', ['$http', function ($http) {
var serviceBase = 'http://localhost:4014/';
var locationsServiceFactory = {};
var _getLocations = function () {
// return a promise
return $http.get(serviceBase + 'api/locations');
};
locationsServiceFactory.getLocations = _getLocations;
return locationsServiceFactory;
}]);
您的观点应该在ng-repeat中引用顺序:
<tr data-ng-repeat="order in orders">
<td>
{{ order.locationId }}
</td>
<td>
{{ order.name }}
</td>
</tr>
答案 2 :(得分:1)
以下内容应该有效:位置ID和名称是订单对象的属性。因此,要访问它们,我们需要以这种方式访问: order.locationId,order.Name
<div class="row">
<div class="col-md-2">
</div>
<div class="col-md-8">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>LocationId</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="order in orders">
<td>
{{ order.locationId }}
</td>
<td>
{{ order.name }}
</td>
</tr>
</tbody>
</table>
</div>
<div class="col-md-2">
</div>
</div>