placeholder issue #26 dev
by chad
HTML
<script src="https://code.angularjs.org/1.3.4/angular.js"></script>
<div ng-init="params={}; params.value='1.00';">
<div formatting-directive="params.value"></div>
</div>
JavaScript
angular.module('theModule', [
'ng.shims.placeholder']);
angular.module('theModule').directive('formattingDirective', function (currencyFilter) {
return {
restrict: 'A',
scope: {
inputValue: '=formattingDirective'
},
template: '<input placeholder="place" ng-model="outputValue" /> {{outputValue}}',
link: function (scope) {
scope.place = 'lala';
scope.outputValue=currencyFilter(scope.inputValue);
}
}
});
angular.module('ng.shims.placeholder', [])
.service('placeholderSniffer', function($document){
this.emptyClassName = 'empty',
this.hasPlaceholder = function() {
// test for native placeholder support
var test = $document[0].createElement('input');
return false;
//return (test.placeholder !== void 0);
};
})
.directive('placeholder', function($timeout, $document, $interpolate, $injector, placeholderSniffer) {
if (placeholderSniffer.hasPlaceholder()) return {};
var documentListenersApplied = false,
angularVersion = parseFloat(angular.version.full);
// load $animate if available, to coordinate with other directives that use it
try {
var $animate = $injector.get('$animate');
} catch (e) {}
// No native support for attribute placeholder
return {
restrict: 'A',
require: '?ngModel',
// run after ngModel (0) and BOOLEAN_ATTR (100) directives.
// priority order was reversed in Angular 1.2, so we must account for this
priority: (angularVersion >= 1.2) ? 110 : -10,
link: function(scope, elem, attrs, ngModel) {
var orig_val = getValue(),
domElem = elem[0],
elemType = domElem.nodeName.toLowerCase(),
isInput = elemType === 'input' || elemType === 'textarea',
is_pwd = attrs.type === 'password',
text = attrs.placeholder || '',
emptyClassName = placeholderSniffer.emptyClassName,
hiddenClassName = 'ng-hide',
clone;
if (!isInput) { return; }
attrs.$observe('placeholder', function (newValue)...