6

在 AngularJS (v1.1.3) 中提交后,我无法重置表单字段。这是我正在尝试做的一个片段:

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

还有其他方法可以重置表单元素吗?

4

2 回答 2

5

感谢@tschiela 的评论。我不得不这样做:

$scope.createMember = function(member) {
    var membersService = new Members(member);
    membersService.$create(function(member) {
        $scope.members.push(member);
        $scope.member = '';
    });
};
于 2013-04-20T20:41:47.643 回答
3

只需取表单的默认值。

HTML 表单

<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>

在控制器内设置重置代码

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

$scope.reset= function(paper) {
$scope.paper = angular.copy(deafualtForm);
}
于 2014-10-10T06:38:47.257 回答