Clear text input on click with AngularJS
How can I clear a text input on click of a button using angularJS?
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>
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;
}
$scope.clearSearch = function () {
$scope.searchAll = "";
};
http://jsfiddle.net/nzPJD/
JsFiddle of how you could do it without using inline JS.
An easier and shorter way is:
<input type="text" class="form-control" data-ng-model="searchAll">
<a class="clear" data-ng-click="searchAll = '' ">X</a>
This has always worked for me.