Created
January 27, 2017 09:32
-
-
Save singhmohancs/ed77306b06a002f8f57ef1ffe7cfdf51 to your computer and use it in GitHub Desktop.
Toggle default value on focus and blur on text field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @ngdoc Directive | |
* @name defaultValue | |
* @restrict 'A' | |
* @element ANY | |
* @author Mohan Singh ( gmail::mslogicmaster@gmail.com, skype :: mohan.singh42 ) | |
**/ | |
(function () { | |
'use strict'; | |
angular | |
.module('tallyfy') | |
.directive('defaultValue', ['_', function (_) { // '_' is a dependency for lodash | |
return { | |
restrict: 'A', | |
require: '^ngModel', | |
link: function (scope, element, attrs, modelCtrl) { | |
var currValue; | |
element.bind('focus', function (event) { | |
if (_.isEmpty(currValue)) { | |
currValue = modelCtrl.$modelValue; | |
} | |
if (currValue === modelCtrl.$modelValue) { | |
modelCtrl.$setViewValue(''); | |
modelCtrl.$render(); | |
} | |
}); | |
element.bind('blur', function (event) { | |
if (_.isEmpty(modelCtrl.$modelValue)) { | |
modelCtrl.$setViewValue(currValue); | |
modelCtrl.$render(); | |
} | |
}); | |
scope.$on('$destroy', function () { | |
element.unbind('focus blur'); | |
}) | |
} | |
} | |
}]); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment