【发布时间】:2016-07-08 23:29:41
【问题描述】:
我有一个带有提交按钮的表单,效果很好。但是,我需要从页面右上角的表单外部的清除按钮清除表单数据。清除按钮存在于父控制器中,位于右上角标题中的表单上方。从清除按钮发送的表单始终显示为未定义,这是因为清除按钮不是表单的一部分。
如何传递相同的表单实例来清除?如何清除数据?如果这是一个设计问题,我仍然需要解决方法。
这是我为模仿而创建的小提琴。任何帮助将不胜感激。
https://jsfiddle.net/SobDan/vj67rtb2/
<div ng-app>
<div class="col-md-10">
<h2>Todo</h2></div>
<div class="col-md-2">
<button class="btn pull-right" ng-click="clear(TodoForm)"> Close</button>
</div>
<br>
<div ng-controller="TodoCtrl">
<form name="TodoForm" ng-submit="addTodo()" name="testForm">
<input type="text" ng-model="todoText" size="30" placeholder="add new todo here">
<input class="btn-primary" type="submit" value="add">
</form>
</div>
function MainCtrl($scope) {
$scope.clear = function(form) {
alert(form); // the form is undefined
if (form.$dirty)
form.setPristine(); // clean this form
else
alert("form not dirty");
};
};
function TodoCtrl($scope) {
$scope.todoText = "test";
$scope.addTodo = function() {
alert("Submitted");
$scope.todoText = "";
// submit logic works fine
};
}
【问题讨论】:
标签: angularjs forms reset form-data