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

Piy*_*ush 128 javascript filter capitalize uppercase angularjs

我想在angularjs中大写字符串的第一个字符

正如我用{{ uppercase_expression | uppercase}}它将整个字符串转换为大写字母.

Nid*_*nan 232

使用此大写过滤器

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

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

app.filter('capitalize', function() {
    return function(input) {
      return (angular.isString(input) && input.length > 0) ? input.charAt(0).toUpperCase() + input.substr(1).toLowerCase() : input;
    }
});
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

  • 如果要意外地将数字大写,则会出现错误.函数的主体应该是这样的:`return(angular.isString(s)&& s.length> 0)?s [0] .toUpperCase()+ s.substr(1).toLowerCase():s;`.如果它不是字符串,则将其保持不变. (11认同)
  • 这是编写的自定义大小写过滤器https://codepen.io/WinterJoey/pen/sfFaK (2认同)

小智 144

我会说不要使用angular/js,因为你只需使用css代替:

在你的CSS中,添加类:

.capitalize {
   text-transform: capitalize;
}
Run Code Online (Sandbox Code Playgroud)

然后,只需在html中包装表达式(对于ex):

<span class="capitalize">{{ uppercase_expression }}</span>
Run Code Online (Sandbox Code Playgroud)

不需要js;)

  • @jakub.g如果你只想把第一个单词(好吧,字母)大写,只需用`:first-letter`伪元素选择器解释css选择器.还有其他事情未被发现?:) (21认同)
  • 此转换将大写***每个单词的第一个字母***因此仅适用于单字符串 (7认同)
  • 对不起,这是一个开玩笑的坏尝试. (4认同)
  • @Philzen是的!仅使用html怎么办?;-) (2认同)

min*_*pop 55

如果使用Bootstrap,则只需添加text-capitalize辅助类:

<p class="text-capitalize">CapiTaliZed text.</p>
Run Code Online (Sandbox Code Playgroud)


Abh*_*nth 25

如果您使用的是Angular 4+,那么您可以使用 titlecase

{{toUppercase | titlecase}}
Run Code Online (Sandbox Code Playgroud)

不必写任何管道.

  • 这是一个错误的答案-问题要求将字符串的首字母大写,而不是每个单词的首字母大写。 (3认同)

Pat*_*der 22

一个更好的方式

app.filter('capitalize', function() {
  return function(token) {
      return token.charAt(0).toUpperCase() + token.slice(1);
   }
});
Run Code Online (Sandbox Code Playgroud)


Rya*_*ews 18

我喜欢@TrampGuy的回答

CSS总是(好吧,并不总是)是一个更好的选择,所以:text-transform: capitalize;肯定是要走的路.

但是如果你的内容一开始就是大写的呢?如果你尝试text-transform: capitalize;像"FOO BAR"这样的内容,你的显示器中仍会显示"FOO BAR".要解决这个问题,你可以把它text-transform: capitalize;放在你的css中,但也可以小写你的字符串,所以:

<ul>
  <li class="capitalize">{{ foo.awesome_property | lowercase }}</li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我们在哪里使用@ TrampGuy的大写类:

.capitalize {
  text-transform: capitalize;
}
Run Code Online (Sandbox Code Playgroud)

因此,假装foo.awsome_property通常打印"FOO BAR",它现在将打印所需的"Foo Bar".


Gre*_*maa 17

如果您在演奏后,请尽量避免使用AngularJS滤镜,因为每个表达式应用两次以检查其稳定性.

更好的方法是使用CSS ::first-letter伪元素text-transform: uppercase;.但是,这不能用于内联元素span,因此,下一个最好的方法是text-transform: capitalize;在整个块上使用,这会占用每个单词.

例:

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

app.controller('Ctrl', function ($scope) {
   $scope.msg = 'hello, world.';
});
Run Code Online (Sandbox Code Playgroud)
.capitalize {
  display: inline-block;  
}

.capitalize::first-letter {
  text-transform: uppercase;
}

.capitalize2 {
  text-transform: capitalize;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
    <div ng-controller="Ctrl">
        <b>My text:</b> <div class="capitalize">{{msg}}</div>
        <p><b>My text:</b> <span class="capitalize2">{{msg}}</span></p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)


Nav*_*raj 14

如果你想从字符串中大写每个单词(进行中 - >正在进行中),你可以像这样使用数组.

.filter('capitalize', function() {
    return function(input) {
        return (!!input) ? input.split(' ').map(function(wrd){return wrd.charAt(0).toUpperCase() + wrd.substr(1).toLowerCase();}).join(' ') : '';
    }
});
Run Code Online (Sandbox Code Playgroud)


pal*_*ukh 11

这是另一种方式:

{{some_str | limitTo:1 | uppercase}}{{some_str.substr(1) | lowercase }}
Run Code Online (Sandbox Code Playgroud)


win*_*mao 9

对于Angular 2及以上,您可以使用{{ abc | titlecase }}.

检查Angular.io API以获取完整列表.


Phi*_*zen 7

.capitalize {
  display: inline-block;
}

.capitalize:first-letter {
  font-size: 2em;
  text-transform: capitalize;
}
Run Code Online (Sandbox Code Playgroud)
<span class="capitalize">
  really, once upon a time there was a badly formatted output coming from my backend, <strong>all completely in lowercase</strong> and thus not quite as fancy-looking as could be - but then cascading style sheets (which we all know are <a href="http://9gag.com/gag/6709/css-is-awesome">awesome</a>) with modern pseudo selectors came around to the rescue...
</span>
Run Code Online (Sandbox Code Playgroud)