【发布时间】:2015-02-20 18:14:29
【问题描述】:
我正在尝试使用 angularjs 和 rails 制作一个带有 ajax 数据的 ng-table。 我看到 http get 已成功获取数据,但由于某种原因,我无法将其附加到我的表中,它总是不显示任何记录。 这是我的代码。 谢谢
(function(){
var app = angular.module("apTags", ["ngTable"]);
app.controller("apTagsController", function($scope, $timeout, $http, ngTableParams){
$http.get('/owners/ap_tags/ap_tags_json.json')
.success(function(data, status) {
$scope.tags = data;
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 10, // count per page
sorting: {name:'asc'}
}, {
total: $scope.tags.length, // length of data
getData: function($defer, params) {
var orderedData = $scope.tags;
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
});
});
})();
<div class="container" ng-app="apTags">
<div ng-controller="apTagsController as list">
<p><strong>Page:</strong> {{tableParams.page()}}</p>
<p><strong>Count per page:</strong> {{tableParams.count()}}</p>
<p>Filter: <input class="form-control" type="text" ng-model="filter.$" /></p>
<table ng-table="tableParams" id="table_tags" class="table table-condensed table-bordered table-hover">
<thead>
<tr>
<th>Tag</th>
<th>Share</th>
<th>Direct Product Count</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="tag in list.tags">
<td >{{tag.name}}</td>
<td >{{tag.share}}%</td>
<td ></td>
</tr>
</tbody>
</table>
</div>
</div>
还有我的 Rails 控制器
class Owners::ApTagsController < WlApplicationController
respond_to :json, only: [:ap_tags_json]
def index
respond_to do |format|
format.json do
@tags = ApTag.all
render :json => @tags.map(&:attributes)
end
format.html
end
end
def ap_tags_json
data = ApTag.all
respond_with(data) do |format|
format.json { render :json => data.map(&:attributes).as_json }
end
end
end
【问题讨论】:
标签: javascript ruby-on-rails angularjs ngtable