Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. ;(function(){
  2. 'use strict'
  3.  
  4. // Sets falsy values as null on the model. This does not seem to be possible
  5. // using ng-false-value, since it sets it as "null" (string) instead.
  6. // usage <input type="checkbox" ng-model"model.attr" null-false-value />
  7.  
  8. angular.module('yourModule')
  9.  
  10. .directive('nullFalseValue', function(_){
  11. return {
  12. require: 'ngModel',
  13. link: function(scope, el, attrs, ctrl){
  14. var ngModel = ctrl
  15. var parserSet
  16.  
  17. function parser(inputVal){
  18. return inputVal || null
  19. }
  20.  
  21. function setParser() {
  22. if (!parserSet) parserSet = ngModel.$parsers.push(parser)
  23. }
  24.  
  25. // Angular 1.2 runs the input directive logic in a post-link function...
  26. // since ngModel requires the input directive, and we require ngModel,
  27. // there is no way to run our $parser after the input directive $parser.
  28. // so the (gulp) hack is to insert it on the first change/focus event.
  29. el.one('focus', setParser)
  30. el.one('change', setParser)
  31. }
  32. }
  33. })
  34. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement