2013-04-20 97 views
6

我在AngularJS(v1.1.3)中提交後重置表單域時遇到問題。下面是我想要做的一個片段:在AngularJS中提交後重置表單

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

有另一種方式來重置表單元素?

+1

設置範圍VAR member.name爲空或空字符串(member.name = 「」) – tschiela 2013-04-20 20:18:49

+0

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

集復位碼這是行不通的。我試圖重置表單中的所有輸入元素,而不是範圍中的數據。 – jesal 2013-04-20 20:23:16

+0

但重置範圍是通用的方法。或者視圖與模型不同步。 – tschiela 2013-04-20 20:25:38

回答

5

感謝@ tschiela的評論。我必須這樣做:

$scope.createMember = function(member) { 
    var membersService = new Members(member); 
    membersService.$create(function(member) { 
     $scope.members.push(member); 
     $scope.member = ''; 
    }); 
}; 
+0

同樣適用於我。我只需在我的表單提交方法中將$ scope.MyObject設置爲null。 – 2013-11-18 21:58:00

2

只需取窗體的默認值。

HTML表格內控制器

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

$scope.reset= function(paper) { 
$scope.paper = angular.copy(deafualtForm); 
}