angularjs push object undefined不是一个函数

时间:2014-08-11 10:04:48

标签: angularjs

所以,我对这段代码有疑问,我不知道为什么会出现这个错误

$scope.veilingen = {};
$http.get('./helpers/get/Veilingen.php').
success(function(data) {
    if(debugMode) console.log(data);
    $scope.veilingenCache = data;
    for(var i = 0; i < 2; i++) {
        console.log($scope.veilingen)
        $scope.veilingen.push({
           test: 'hello'
        });
        console.log($scope.veilingen)
    }
});

我收到此错误:

  

TypeError:undefined不是函数       在http://example.com/includes/controllers/veilingCntrl.js:12:30       在http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.min.js:72:72       在我(http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.min.js:100:144)       在我(http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.min.js:100:144)       在http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.min.js:101:308       at k。$ eval(http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.min.js:112:32)       at k。$ digest(http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.min.js:109:121)       at k。$ apply(http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.min.js:112:362)       在h(http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.min.js:72:327)       在x(http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.18/angular.min.js:77:288

第12行是push()区域。我已经搜索了为什么会发生这种情况,但我找不到它。我想要的是veilingen对象得到前2个结果,加载后用户可以改变它:

$scope.setLimitVeiling = function(items) {
    for(var i = 0; i < items; i++) {
        $scope.veilingen.push($scope.veilingenCache[i]);
    }
}

但是,如果推动不起作用......?

1 个答案:

答案 0 :(得分:13)

$scope.veilingen被声明为对象而不是数组。

$scope.veilingen = [];