6

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?

4
  • 1
    Set the scope var member.name to null or empty string (member.name="") Commented Apr 20, 2013 at 20:18
  • Hm that doesn't work. I'm trying to reset all the input elements in the form not the data in the scope. Commented Apr 20, 2013 at 20:23
  • but to reset the scope is the common way to that. Or the view is not in sync to the model. Commented Apr 20, 2013 at 20:25
  • Ah ok I think you are right, I had to do $scope.member = ''; Commented Apr 20, 2013 at 20:35

2 Answers 2

5

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 = '';
    });
};
Sign up to request clarification or add additional context in comments.

1 Comment

The same worked for me. I simply set $scope.MyObject to null in my form submit method.
3

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.