Skip to content

Instantly share code, notes, and snippets.

@pezza3434
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pezza3434/7b9f840e905c673a24f2 to your computer and use it in GitHub Desktop.
Save pezza3434/7b9f840e905c673a24f2 to your computer and use it in GitHub Desktop.
Example of a directive using parsers and formatters
ngModel.$parsers.push(function(value){
value.toUpperCase();
return value;
});
$scope.change_model = function(){
$scope.some_letters = {value: 'dave'};
};
$scope.some_letters = {value:'alex'};
app.directive('changeCase', function(){
return{
restrict: 'A',
templateUrl: 'scripts/directives/directive_templates/directive.html',
require: 'ngModel',
link: function(scope, element, attr, ngModel){
ngModel.$formatters.push(function(value){
});
ngModel.$parsers.push(function(value){
});
}
};
});
<form role="form" name="myform">
<div class="form-group">
<label>View Value:</label>
<input name="someinput" changecase="" ng-model="some_letters.value">
</div>
</form>
<strong>ModelValue:</strong> {{some_letters.value}} <br>
var view_value;
ngModel.$parsers.push(function(value){
var return_value;
if(value.length > 5){
return_value = view_value;
ngModel.$setViewValue(view_value);
ngModel.$render();
ngModel.$setValidity('is_valid', false);
} else {
return_value = value;
view_value = return_value;
ngModel.$setValidity('is_valid', true);
}
return return_value;
});
ngModel.$formatters.push(function(value){
value.toUpperCase();
return value;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment