Skip to content

Commit 0698e28

Browse files
author
Vikas Gulatii
committed
add polyfill directive js
1 parent b1ed03d commit 0698e28

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
angularjs-placeholder.js
22
========================
33

4-
AngularJs directive pollyfill for html5 placeholder without jQuery dependency.
4+
AngularJs directive pollyfill for html5 placeholder *without jQuery dependency*.
5+
Inspired from https://gist.github.com/veeracs/5770791.
6+
7+
Requires
8+
==========================
9+
1. Angularjs - obviously!
10+
1. Modernizr.js with placeholder support detection (Get your custom build here http://modernizr.com/download/#-input-inputtypes-forms_placeholder)
11+
12+
13+
Installation
14+
==========================
15+
1. Download the angularjs-placeholder-polyfill.js file.
16+
1. Include it in your site.
17+
1. Include Modernizr in your site.
18+
1. Install customPlaceholder module in your angular app.
19+

angularjs-placeholder-polyfill.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
angular.module('customPlaceholder', []).directive('placeholder', function(){
2+
// cn-placeholder directive definition object
3+
return {
4+
restrict: 'A',
5+
require: 'ngModel',
6+
link: function(scope, elm, attrs, ctrl) {
7+
if (!Modernizr.placeholder) {
8+
// setup the label overlay for input
9+
var computedStyle = window.getComputedStyle(elm[0], null),
10+
leftPadding = computedStyle.getPropertyValue('padding-left'),
11+
topPadding = computedStyle.getPropertyValue('padding-top'),
12+
placeholderLabel = angular.element(elm.parent()
13+
.css({'position':'relative'})
14+
.prepend('<span>' + elm.attr('placeholder') + '</span>')
15+
.find('span')[0])
16+
.css({ 'color':'#999',
17+
'position':'absolute',
18+
'top': topPadding,
19+
'left':leftPadding}),
20+
placeholderBindClick = function(){
21+
placeholderLabel.bind('click', function(){
22+
elm[0].focus();
23+
});
24+
};
25+
26+
placeholderBindClick();
27+
28+
// removes the label overlay when a value is typed
29+
var elemPlaceHandlePlaceholder = function(){
30+
if (elm.val() !== '') {
31+
placeholderLabel.remove();
32+
} else {
33+
elm.parent().prepend(placeholderLabel);
34+
placeholderBindClick();
35+
}
36+
};
37+
38+
scope.$watch(attrs.ngModel, function(newValue, oldValue) {
39+
elemPlaceHandlePlaceholder();
40+
});
41+
42+
elm.bind('keyup keydown', function(){
43+
elemPlaceHandlePlaceholder();
44+
});
45+
}
46+
}
47+
};
48+
});

0 commit comments

Comments
 (0)