user3718908x100
user3718908x100

Reputation: 8509

Posting Objects via $http

Hello i am a bit confused about something, when i post my form the data does not get sent to the server, i've checked my console several time and i can't understand what is happening, this is my controller:

angular.module('app').controller('InstCtrl', ['$http', '$scope', '$state', 'InstService', function($http, $scope, $state, InstService) {

    var parameters = {
        name: $scope.name,
        description: $scope.description,
        email: $scope.email,
        phone: $scope.phone,
        website: $scope.website,
        isActive: $scope.isActive
    };

    $scope.addInstitution = function(parameters) {

        InstService.add(parameters);
        console.log(parameters);

    };

}]);

This is the service:

angular.module('app').factory('InstService', function ($http) {

    var InstService = {};

    InstService.add = function(parameters) {

        return $http
            .post('/api/v1/institutions/', parameters)

    };

    return InstService;
});

I use AngularJS on the front end and laravel 5 on my server.

console.log returns undefined and my $http.post is always empty, as in no data is being posted to the server.

Upvotes: 0

Views: 30

Answers (1)

lostintranslation
lostintranslation

Reputation: 24563

Where do you actually call the addInstitution method? Assuming its coming from a button click in your html. If so I don't think this will work. You define the parameters object before those scope variables are set.

Try moving the parameters definition into the addInstitution:

$scope.addInstitution = function() {
   var parameters = {
       name: $scope.name,
       description: $scope.description,
       email: $scope.email,
       phone: $scope.phone,
       website: $scope.website,
       isActive: $scope.isActive
    };

    console.log(parameters);

    InstService.add(parameters);
};

Upvotes: 1

Related Questions