且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在AngularJs中大写字母的第一个字母

更新时间:2023-02-05 14:12:03

use this capitalize filter



  var app = angular.module('app',[]); app.controller('Ctrl' msg ='hello,world。';}); app.filter('capitalize',function(){return function(input){return(!! input)?input.charAt(0).toUpperCase()+ input。 substr(1).toLowerCase():'';}});  

 < script src =https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js>< / script>< div ng-app = 应用程序 &GT; < div ng-controller =Ctrl> < p>< b>我的文字:< / b> {{msg |利用}}&LT; / p为H. < / div>< / div>  

I want capitalize first character of string in angularjs

As i used {{ uppercase_expression | uppercase}} it convert whole string to upper case.

use this capitalize filter

var app = angular.module('app', []);

app.controller('Ctrl', function ($scope) {
   $scope.msg = 'hello, world.';
});

app.filter('capitalize', function() {
    return function(input) {
      return (!!input) ? input.charAt(0).toUpperCase() + input.substr(1).toLowerCase() : '';
    }
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
    <div ng-controller="Ctrl">
        <p><b>My Text:</b> {{msg | capitalize}}</p>
    </div>
</div>