Angular:
http://angularjs.org/
HTML
<div ng-controller="MyCtrl">
<input type="text" ng-model="name" capitalize-first>
<input type="text" ng-model="obj.name" capitalize-first>
</div>
JavaScript
var myApp = angular.module('myApp', []);
myApp.directive('capitalizeFirst', function (uppercaseFilter, $parse) {
return {
require: 'ngModel',
link: function (scope, element, attrs, modelCtrl) {
var capitalize = function (inputValue) {
let first = element[0].selectionStart;
let last = element[0].selectionEnd;
console.log('aaa',first, last);
if (inputValue != null) {
var capitalized = inputValue.charAt(0).toUpperCase() + inputValue.substring(1);
if (capitalized !== inputValue) {
modelCtrl.$setViewValue(capitalized);
modelCtrl.$render();
}
element[0].selectionStart = first;
element[0].selectionEnd = last;
return capitalized;
}
};
var model = $parse(attrs.ngModel);
modelCtrl.$parsers.push(capitalize);
capitalize(model(scope));
}
};
});
function MyCtrl($scope) {
$scope.name = '';
$scope.obj = {
name: ''
};
}