Atlante Avila Atlante Avila - 6 months ago 19
Javascript Question

Angularjs forms not clearing on submit

I'm trying to clear my angularjs form after form submission. I've got the form to submit properly but within my function to submit the form, I've tried clearing the input fields of my form by targeting the

$scope.[name of field here] = '';

Can someone help me understand why?

Here's my code for the submit function in my controller:

$scope.submitOwner = function(){
$scope.firstName = '';
$scope.lastName = '';
$scope.age = '';
$scope.location = '';
$scope.favorite = '';
$scope.numberOfBreeds = '';
$scope.numberOfDogs = '';
Owner.create($scope.owner).success( function(){
console.log('User created!')

Answer Source

So I made the changes that Paulo suggested in the comments and got it to work! The form was using the 'owner' model and I simply changed the fields to target the ng-model name within the scope and got it to clear successfully! Here's the updated code:

  $scope.owner = {};

now the form successfully clears. Thanks for clearing that up for me Paulo!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download