Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset form after submission in AngularJS

I'm having trouble resetting form fields after submitting in AngularJS (v1.1.3). Here's a snippet of what I'm trying to do:

HTML

<form name="addMemberForm">
    <input name="name" type="text" placeholder="Jon Doe" ng-model="member.name" required/></td>
    <a class="btn btn-primary" ng-click="createMember(member)" ng-disabled="addMemberForm.$invalid"><i class="icon-plus"></i></a>
</form>

JS

$scope.createMember = function(member) {
    var membersService = new Members(member);
    membersService.$create(function(member) {
        $scope.members.push(member);
        $scope.addMemberForm.reset(); //TypeError: Object #<FormController> has no method 'reset'
    });
};

Is there another way to reset form elements?

like image 654
jesal Avatar asked Apr 20 '13 19:04

jesal


People also ask

How to reset form after submit in Angular?

import { FormsModule } from '@angular/forms'; In Reactive forms, we need to import FormGroup from '@angular/forms' . After importing the above-mentioned modules in the respective approach, angular forms module provides an inbuilt method called reset(). We can use the method and we can reset the form.

How to reset the form in AngularJS?

In a model-driven form to reset the form we just need to call the function reset() on our myform model. The form now resets, all the input fields go back to their initial state and any valid , touched or dirty properties are also reset to their starting values.

How do I reset form values?

reset() method restores a form element's default values. This method does the same thing as clicking the form's <input type="reset"> control. If a form control (such as a reset button) has a name or id of reset it will mask the form's reset method. It does not reset other attributes in the input, such as disabled .


2 Answers

Figured it out thanks to @tschiela's comment. I had to do this:

$scope.createMember = function(member) {
    var membersService = new Members(member);
    membersService.$create(function(member) {
        $scope.members.push(member);
        $scope.member = '';
    });
};
like image 115
jesal Avatar answered Sep 22 '22 12:09

jesal


Just Take default value of the form .

HTML form

<form novalidate id="paperForm"  class="form-horizontal" name="formPaper">
<div class="form-group">
<label class="col-sm-3 control-label" for="name">
    Name
</label>
<div class="col-sm-8">
    <input type="text" id="name" placeholder="Please Enter Name"  class="form-control" ng-model="paper.name" ng-name="name" required>
</div>

<label class="col-sm-3 control-label" for="name">
    Department
</label>
<div class="col-sm-8">
    <input type="text" id="department" placeholder="Please Enter Department"  class="form-control" ng-model="paper.department" ng-name="department" required>
</div>

</div>

 <button type="button" class="btn btn-default" ng-click="reset(paper)">Reset</button>

</form>

set reset code inside controller

var deafualtForm = {
name : '',
department : ''
}

$scope.reset= function(paper) {
$scope.paper = angular.copy(deafualtForm);
}
like image 20
sourcecode Avatar answered Sep 21 '22 12:09

sourcecode