2016-02-09 7 views
1

Когда я отправляю деталь в api с angularJs, у меня появилась ошибка, например, 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'); 
}; 
}]); 

ответ

1

Вы не определили $scope.posts как массив в любом месте. Выше вашего определения $scope.create просто введите инициализационный отчет, например

$scope.posts = []; 
Смежные вопросы