Created
November 19, 2014 08:50
-
-
Save zaltoprofen/5e84f2bcac2a20a3dcde to your computer and use it in GitHub Desktop.
This file contains hidden or 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
app = angular.module 'hoge' | |
app.directive 'json', -> | |
{ | |
require: 'ngModel' | |
link: (scope, elm, attr, ctrl) -> | |
ctrl.$parsers.push (value) -> | |
try | |
parsed = angular.fromJson(value) | |
isValid = true | |
catch error | |
isValid = false | |
ctrl.$setValidity('json', isValid) | |
return if isValid then parsed else undefined | |
ctrl.$formatters.push (value) -> | |
try | |
dumped = angular.toJson(value) | |
isValid = true | |
catch error | |
isValid = false | |
ctrl.$setValidity('json', isValid) | |
return if isValid then dumped else undefined | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment