1. approach:
I would suggest doing it when receiving the data.
function addThirdCharacter(arr) {
// your logic of adding the character
}
$http({
method: 'GET',
url: '/states'
}).then(function successCallback(response) {
$scope.states = addThirdCharacter(response)
}, function errorCallback(response) {
// Error handling
});
2nd approach
$formatters for ngModel: https://docs.angularjs.org/api/ng/type/ngModel.NgModelController
angular.module('myApp', ['ngSanitize']).
directive('addCharacter', ['$sce', function($sce) {
return {
restrict: 'A', // only activate on element attribute
require: '?ngModel', // get a hold of NgModelController
link: function(scope, element, attrs, ngModel) {
if (!ngModel) {
return; // do nothing if no ng-model
}
function formatter(value) {
if (value) {
return addThirdCharacter(value);
}
}
ngModel.$formatters.push(formatter);
}
};
}]);
3rd approach
If that's not an option, I would add a custom directive for that, that show the correct value for each 2 character (look at viewValue
and modelValue
if you have ngModel available)
Documentation about directives: https://docs.angularjs.org/guide/directive
If you are new to directives I would suggest you this very very good tutorial post: http://www.ng-newsletter.com/posts/directives.html