当我使用 angularJs 将详细信息发布到 api 时,出现类似 TypeError: $scope.posts.push is not a function
的错误,现在我该如何解决这个问题?
我的邮政编码是:
AdminApp.controller('AddVehicleModalCtrl', ['$scope', '$rootScope', '$modalInstance','$http','toaster', function ($scope, $rootScope, $modalInstance,$http,toaster) {
$scope.create = function(){
$http.post(AppCore.getGlobalApiUrl()+'vehicle_type', {"vehicle_type": $scope.post.vehicle_type})
.success(function(response, status, headers, config){
$modalInstance.dismiss('cancel');
toaster.pop('success', "post", "done");
$scope.posts.push(response.posts);
$scope.$apply();
})
.error(function(response, status, headers, config){
$scope.error_message = response.error_message;
});
};
$scope.cancel = function () {
$scope.post = "";
$modalInstance.dismiss('cancel');
};
}]);
最佳答案
您尚未在任何地方将 $scope.posts
定义为数组。在您的 $scope.create
定义之上,只需添加一个初始化语句,例如
$scope.posts = [];
关于javascript - AngularJs TypeError : $scope. posts.push 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35297213/