AngularJS - Format Text Return From JSON To Title Case

前端 未结 3 470
离开以前
离开以前 2021-02-03 20:29

I have a service that retrieves data from a JSON file.

Some of the data within the data is all in uppercase, for example:

$scope.FootballClubs = [{
    C         


        
3条回答
  •  情歌与酒
    2021-02-03 21:05

    A filter is an ideal solution for this purpose

    {{ name.CompanyName | titleCase }}

    So the filter itself would be

    angular.module('myFootballModule', [])
      .filter('titleCase', function() {
        return function(input) {
          input = input || '';
          return input.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
        };
      })
    

提交回复
热议问题