AngularJS – isString() 方法

angularjsweb developmentfront end technology更新于 2026/1/7 2:52:17

AngularJS 中的 isString() 方法主要用于检查引用是否为字符串值。如果函数内部传递的引用是字符串,则此方法返回 True,否则返回 False。

语法

angular.isString(value)

示例 − 检查引用是否为字符串

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

<!DOCTYPE html>
<html>
   <head>
      <title>angular.isString()</title>

      <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
      </script>
   </head>

   <body ng-app="app" style="text-align:center">
      <h1 style="color:green">
         Welcome to Tutorials Point
      </h1>
      <h2>AngularJS | angular.isString()</h2>

      <div ng-controller="example">
         <b>Name: {{name}}</b>
         <br><br>
         {{isString}}
         <br><br>
         <b>Name: {{name2}}</b>
         <br><br>
         {{isString1}}
         <br><br>
         <b>Name: {{name3}}</b>
         <br><br>
         {{isString2}}
      </div>

      <!-- Script for passing the values and checking... -->
      <script>
         var app = angular.module("app", []);
         app.controller('example',['$scope', function ($scope)
         {
            // Defining the keys & values
            $scope.name = "SIMPLY LEARNING";
            $scope.name2 = "";
            $scope.name3 = {"name": "tutorialsPoint"};

            $scope.isString = angular.isString($scope.name) == true
                ? "$scope.name is a String."
                : "$scope.name is not a String.";

            $scope.isString1 = angular.isString($scope.name2) == true
               ? "$scope.name2 is a String."
               : "$scope.name2 is not a String.";

            $scope.isString2 = angular.isString ($scope.name3) == true
               ? "$scope.name2 is a String."
               : "$scope.name2 is not a String.";
         }]);
      </script>
   </body>
</html>

输出

要运行上述代码,只需打开您的文件并将其作为普通 HTML 文件运行即可。您将在浏览器窗口中看到以下输出。

请注意,在给定代码中,name 和 name2 是字符串,而 name3 是键值对,这就是为什么我们得到输出"$scope.name2 is not a String"。


相关文章