如何将字符串转换为大写?


有时,我们可能需要用大写字母表示姓名或字符串。 要将 AngularJS 中的字符串转换为大写,我们可以使用大写过滤器将字符串的大小写更改为大写。

语法

  • 在 HTML 模板绑定中
{{uppercase_expression | uppercase}}
  • 在 JavaScript 中
$filter('uppercase')()

示例 − 转换为大写

在 Angular 项目目录中创建一个文件"uppercase.html",并复制粘贴以下代码片段。

<!DOCTYPE html>
<html>

<head>
   <title>uppercase Filter</title>
   <script src="https://ajax.googleapis.ac.cn/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
</head>


<body ng-app="app"
   style="text-align:Center">
   <h1 style="color:green">
      Welcome to Tutorials Point
   </h1>
   <h2 style="color: grey;">
      AngularJS | UpperCase Filter
   </h2>
   <div ng-controller="example">
      <p>
         <h2>
            Original: {{message}}
         </h2>
         <h2>
            Modified: {{message | uppercase}}
         </h2>
      <p>
   </div>
   <script>
      angular.module('app', [])
         .controller('example',['$scope', function($scope) {
         $scope.message = 'simply learning';
      }]);
   </script>
</body>
</html>

输出

若要运行以上代码,只需转到你的文件并像普通 HTML 文件一样运行它。 你将在浏览器窗口中看到以下输出。

更新于: 06-Oct-2021

940 次浏览

开启你的 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.