Bar*_*ney 45 javascript forms angularjs
如何使用angularJS清除单击按钮时的文本输入?

X是一个单独的链接,我想在其上触发一个函数.
HTML
<input type="text" class="form-control" data-ng-model="searchAll">
<a class="clear" data-ng-click="clearSearch()">X</a>
Run Code Online (Sandbox Code Playgroud)
Rob*_*nik 83
只需清除点击事件中的范围模型值,它应该为您完成.
<input type="text" ng-model="searchAll" />
<a class="clear" ng-click="searchAll = null">
<span class="glyphicon glyphicon-remove"></span>
</a>
Run Code Online (Sandbox Code Playgroud)
或者,如果您保留控制器的$scope功能并从那里清除它.确保已正确设置控制器.
$scope.clearSearch = function() {
$scope.searchAll = null;
}
Run Code Online (Sandbox Code Playgroud)
Kas*_*wau 15
$scope.clearSearch = function () {
$scope.searchAll = "";
};
Run Code Online (Sandbox Code Playgroud)
JsFiddle如何在不使用内联JS的情况下完成它.
Shi*_*mar 10
更简单,更简单的方法是:
<input type="text" class="form-control" data-ng-model="searchAll">
<a class="clear" data-ng-click="searchAll = '' ">X</a>
Run Code Online (Sandbox Code Playgroud)
这一直对我有用.
如果要清理整个表单,可以使用这种方法.这是你的控制器模型:
$scope.registrationForm = {
'firstName' : '',
'lastName' : ''
};
Run Code Online (Sandbox Code Playgroud)
你的HTML:
<form class="form-horizontal" name="registrForm" role="form">
<input type="text" class="form-control"
name="firstName"
id="firstName"
ng-model="registrationForm.firstName"
placeholder="First name"
required> First name
<input type="text" class="form-control"
name="lastName"
id="lastName"
ng-model="registrationForm.lastName"
placeholder="Last name"
required> Last name
</form>
Run Code Online (Sandbox Code Playgroud)
然后,您应该克隆/保存您的清除状态:
$scope.originForm = angular.copy($scope.registrationForm);
Run Code Online (Sandbox Code Playgroud)
您的重置功能将是:
$scope.resetForm = function(){
$scope.registrationForm = angular.copy($scope.originForm); // Assign clear state to modified form
$scope.registrForm.$setPristine(); // this line will update status of your form, but will not clean your data, where `registrForm` - name of form.
};
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以清理整个表单
单击时清除/重置文本字段的最简单方法是清除/重置范围
<input type="text" class="form-control" ng-model="searchAll" ng-click="clearfunction(this)"/>
Run Code Online (Sandbox Code Playgroud)
在控制器中
$scope.clearfunction=function(event){
event.searchAll=null;
}
Run Code Online (Sandbox Code Playgroud)