AngularJS - Format Text Return From JSON To Title Case

允我心安 提交于 2019-12-02 23:25:58
dubadub

A filter is an ideal solution for this purpose

<div ng-repeat="name in FootballClubs">
    {{ name.CompanyName | titleCase }}
</div>

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();});
    };
  })

Let me offer a non-directive way which is probably easier to implement but less powerful and dependent on the solution being UI only. First, change them to lowercase {{name.CompanyName.toLowerCase()}}

I think the easiest way is to just have CSS format it (either via a style tag, class, etc).

<div ng-repeat="name in FootballClubs" style="text-transform:capitalize;">
 {{ name.CompanyName.toLowerCase() }}
</div>

Here is a demo: http://plnkr.co/edit/S4xtIRApMjKe0yQGREq5?p=preview

just use this div in your HTML

  <div ng-repeat="name in FootballClubs">
     {{ name.CompanyName | titleCase:CompanyName }}
 </div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!