Last active
August 29, 2015 14:15
-
-
Save andresmatasuarez/36fe2f7a801a288bdc08 to your computer and use it in GitHub Desktop.
AngularJS | Directive | Makes an input fall back to a default value in case of being empty.
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
'use strict' | |
app = angular.module 'app' | |
app.directive 'defaultValue', -> | |
require : 'ngModel' | |
restrict : 'A' | |
link : ($scope, element, attrs, ngModel) -> | |
ngModel.$parsers.push (value) -> | |
if _.isEmpty value | |
value = attrs.defaultValue | |
ngModel.$setViewValue value | |
ngModel.$render() | |
value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment