Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear text input on click with AngularJS

How can I clear a text input on click of a button using angularJS?

Search input

The X is a seperate link, on which I would like to trigger a function.

HTML

<input type="text" class="form-control" data-ng-model="searchAll"> <a class="clear" data-ng-click="clearSearch()">X</a> 
like image 579
Barney Avatar asked Feb 11 '14 17:02

Barney


People also ask

How to clear input field on button click in Angular?

If you are using [(ngModel)] directive to control your form input fields, then you can clear it by setting an empty string ( ' ' ) to the ngModel control property.

How do you clear form data after submit in Angularjs?

1) To Remove the values in Form Fields and to reset you can use $setPristine(); $scope. formName. $setPristine();

How do you delete a form in Angularjs?

resetForm = function(){ /* reset the data to a new object so that all the properties * of form are reset */ $scope. data = {}; }; });


2 Answers

Just clear the scope model value on click event and it should do the trick for you.

<input type="text" ng-model="searchAll" /> <a class="clear" ng-click="searchAll = null">     <span class="glyphicon glyphicon-remove"></span> </a> 

Or if you keep your controller's $scope function and clear it from there. Make sure you've set your controller correctly.

$scope.clearSearch = function() {     $scope.searchAll = null; } 
like image 139
Robert Koritnik Avatar answered Oct 11 '22 23:10

Robert Koritnik


$scope.clearSearch = function () {     $scope.searchAll = ""; }; 

http://jsfiddle.net/nzPJD/

JsFiddle of how you could do it without using inline JS.

like image 41
Kasper Lewau Avatar answered Oct 11 '22 22:10

Kasper Lewau