Skip to content

Instantly share code, notes, and snippets.

@andresmatasuarez
Last active August 29, 2015 14:15
Show Gist options
  • Save andresmatasuarez/36fe2f7a801a288bdc08 to your computer and use it in GitHub Desktop.
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.
'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