AngularJS – isString() method


The isString() method in AngularJS basically checks if a reference is a string value or not. This method will return True if the reference passed inside the function is a string, else it will return False.

Syntax

angular.isString(value)

Example − Check if the reference is a String or not

Create a file "isString.html" in your Angular project directory and copy-paste the following code snippet.

<!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>

Output

To run the above code, just go to your file and run it as a normal HTML file. You will see the following output on the browser window.

Observe that name and name2 are strings, whereas name3 is a key-value pair in the given code, which is why we got the output "$scope.name2 is not a String."

Updated on: 08-Oct-2021

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements