是的,你需要定义一个指令并定义自己的解析器函数:
myApp.directive('capitalizeFirst',function($parse) { return { require: 'ngModel',link: function(scope,element,attrs,modelCtrl) { var capitalize = function(inputValue) { if (inputValue === undefined) { inputValue = ''; } var capitalized = inputValue.charAt(0).toUpperCase() + inputValue.substring(1); if(capitalized !== inputValue) { modelCtrl.$setViewValue(capitalized); modelCtrl.$render(); } return capitalized; } modelCtrl.$parsers.push(capitalize); capitalize($parse(attrs.ngModel)(scope)); // capitalize initial value } }; });
HTML:
<input type="text" ng-model="obj.name" capitalize-first>