AngularJS update record in database from form

0

I need the logic to correctly save or update (POST) a set of records. Using this resource that calls the database.

TripResource.updateTrip = function(id,ship){
    return $http.put(URL + '/trips'+'/'+id, trip);
    // .success(function(data, status, headers, config){

    // })
};

I understand that you would have to put in a function in the controller, for that function to put it in the Save button in a ng-click ... But the function expects an id and a trip with the info of the whole object . I also know that I have to declare an empty object like:

$scope.updateTrip={};

But how are the form values sent to that object?

    
asked by Gabo Ruiz 21.06.2017 в 00:10
source

1 answer

0

Good, to be able to take the data of a form, you can use ng-model:

Html:

<form>
  <label for="">Nombre</label>
  <input type="text" name="" id="" value="" ng-model='nombre'/>

  <label for="">Apellido</label>
  <input type="text" name="" id="" value="" ng-model='apellido'/>

  <button ng-click='guardar()'>Guardar</button>
</form>

Controller:

$scope.guardar= function(){
    servicio.setNombres({nombre: $scope.nombre, apellido: $scope.apellido}).then(function(data){
      $scope.ultimoId = data.data;
    });
  }

Service:

function setNombres(valor){
    return $http.post('/nombres', valor)
    .then(function(response){ return response; })
    .catch(function(response){ return response; });
  }

In this plnkr you can see it working: link

The backend service is simulated so you can receive a response.

I hope that is what you are looking for, and do not hesitate to ask again otherwise. Greetings

    
answered by 21.06.2017 в 03:42