gpt4 book ai didi

javascript - TypeError : Object # has no method 'push' in Angular js Http. 获取请求
转载 作者:行者123 更新时间:2023-11-30 12:51:43 27 4
gpt4 key购买 nike

以下是我的 Controller ,它非常简单地尝试在单击按钮时提取数据,并尝试将其推送到 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' 

怎么了?顺便说一句,我是一个完整的 Angular 菜鸟。服务器返回的 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"
}
},...................

最佳答案

问题是 $scope.posts 不是一个数组(或者任何东西,你不能只是 push 到一个 undefined variable 并期望一个数组来其中)。我会在 Controller 的开头或回调的开头定义一个空数组:

$scope.posts = [];

关于javascript - TypeError : Object #<Object> has no method 'push' in Angular js Http. 获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20746123/

27 4 0