-
Notifications
You must be signed in to change notification settings - Fork 5
/
angular-clear-button.js
38 lines (36 loc) · 1.48 KB
/
angular-clear-button.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* @author Tushar Borole
* @description Add ios style clear button for input box
* for example <input type="text" id="fixed" clear-btn/>
*/
angular.module('angular-clear-button', []).directive('clearBtn', ['$parse', function ($parse) {
return {
link: function (scope, elm, attr, ngModelCtrl) {
var top = elm.height() / 2;
elm.wrap("<div style='position: relative'></div>");
var btn = '<span id=' + Math.round(Math.random() * 1000000000) + ' class="searchclear ng-hide glyphicon glyphicon-remove-circle"></span>';
var angularBtn = angular.element(btn);
angularBtn.css('top', top);
elm.after(angularBtn);
//clear the input
angularBtn.on("click", function () {
elm.val('').trigger("change");
$parse(attr.ngModel).assign(scope, null);
scope.$apply();
});
// show clear btn on focus
elm.bind('focus keyup change paste propertychange', function (blurEvent) {
if (elm.val() && elm.val().length > 0) {
angularBtn.removeClass("ng-hide");
} else {
angularBtn.addClass("ng-hide");
}
});
// remove clear btn on focus
elm.bind('blur', function (blurEvent) {
if (!angularBtn.is(":hover"))
angularBtn.addClass("ng-hide");
});
}
};
}]);