我想在單擊保存按鈕後清空表單域,但無法做到這一點。單擊保存按鈕後,如何清空表單域
這裏是我的完整code
:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.oData = {};
$scope.fnSave = function(data) {
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<form name="myForm">
<div class="form-group col-sm-12 col-xs-12">
<label class="control-label">Name</label>
<input class="form-control col-xs-12" type='text' ng-model="oData.name" required>
</div>
<div class="form-group col-sm-12 col-xs-12">
<label class="control-label">Practice</label>
<input class="form-control col-xs-12" type='text' ng-model="oData.practice" required>
</div>
<div class="form-group col-sm-12 col-xs-12">
<label class="control-label">designation</label>
<input class="form-control col-xs-12" type='text' ng-model="oData.designation" required>
</div>
<button ng-click="fnSave(oData)">save</button>
</form>
</body>
</html>
@Ven或者[他們確實](http://s2.quickmeme.com/img/15/1599e06bd7b11b9d1e3d414c4cfb97bd3ad4ed044ac06d4ec5625cf4d0d224ec.jpg):P – Borgleader
請參閱我編輯的問題@Ven –
正確的方法是使用模型目的。因此,不是使用'$ scope.firstName','$ scope.lastName'等,而是使用'$ scope.formModel.firstName','$ scope.formModel.lastName'等等。然後當你想清除一些東西您只需使用'$ scope.formModel = {};'並且所有內容都已清空並準備好用於新輸入。 – Lex