Last active
August 29, 2015 14:11
-
-
Save pezza3434/7b9f840e905c673a24f2 to your computer and use it in GitHub Desktop.
Example of a directive using parsers and formatters
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
ngModel.$parsers.push(function(value){ | |
value.toUpperCase(); | |
return value; | |
}); |
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
$scope.change_model = function(){ | |
$scope.some_letters = {value: 'dave'}; | |
}; |
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
$scope.some_letters = {value:'alex'}; |
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
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){ | |
}); | |
} | |
}; | |
}); |
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
<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> |
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
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; | |
}); |
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
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