【发布时间】:2014-01-11 19:47:15
【问题描述】:
以下是我的控制器,它非常简单地尝试在单击按钮时提取数据,并尝试将其推入 ng-repeat。
app.controller('HomeController',function($scope,$http) {
$scope.user = [];
$scope.posts = [];
$http.get('http://hashtag.dev/api/v1/user').success(function(data) {
$scope.user = data;
});
$http.get('http://hashtag.dev/api/v1/post').success(function(data) {
$scope.posts = data;
});
$scope.loadmore = function() {
$http.get('http://hashtag.dev/api/v1/post').success(function(data) {
$scope.posts.push(data);
});
};
});
当点击带有 ng-click="loadmore()" 的按钮时
TypeError: Object #<Object> has no method 'push'
怎么了?顺便说一句,我是一个完整的角菜鸟。服务器返回的 JSON..
{
"total": 37,
"per_page": 10,
"current_page": 2,
"last_page": 4,
"from": 11,
"to": 20,
"data": [
{
"id": 84,
"user_id": 4,
"post_text": "Sunt eius voluptatem nostrum eos eos ipsa qui. Laudantium ratione repudiandae vitae sunt distinctio earum.",
"created_at": "2013-12-15 14:29:00",
"deleted_at": null,
"user": {
"id": 4,
"full_name": "Vinnie Lang",
"username": "vinnie398",
"created_at": "2013-12-15 14:28:54"
}
},
{
"id": 82,
"user_id": 2,
"post_text": "Laudantium minus animi alias dolorum aperiam non. Odit quidem doloribus nihil eius incidunt sint nulla.",
"created_at": "2013-12-15 14:29:00",
"deleted_at": null,
"user": {
"id": 2,
"full_name": "Shyanne Champlin",
"username": "shyanne718",
"created_at": "2013-12-15 14:28:53"
}
},...................
【问题讨论】:
-
将
console.log($scope.posts);放入 loadmore 并检查您的控制台。 -
为什么
$scope.posts = []被注释掉了?在我看来,取消注释可以解决问题。 -
哦,我这样做仍然不起作用,我正在添加响应,即 JSON。
-
@Tejas -
console.log($scope)- 它应该是一个带有posts数组和user数组的对象 - 是吗?
标签: javascript angularjs laravel-4 eloquent