AngularJS – equals() method

AngularJSWeb DevelopmentFront End Technology

The equals() method in AngularJS basically checks if two objects or two values are equal or not. This method supports value types, regular expressions, arrays, and objects. It will return True if the reference objects passed inside the function are equal, else it will return False.

Syntax

angular.equals(value1, value2)

Example − Check if the reference objects are equal or not

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

<!DOCTYPE html>
<html>
   <head>
      <title>angular.equals()</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 Points
      </h1>
      <h2>AngularJS | angular.equals()</h2>

      <div ng-controller="demo">
         Input A: <input type="number" ng-model="val1" ng-change="check()" /> <br />
         <br />
         Input B: <input type="number" ng-model="val2" ng-change="check()" /> <br />
         <br />
         {{msg}}
      </div>

      <!-- Script for passing the values and checking... -->
      <script>
         var app = angular.module("app", []);
         app.controller("demo", [
            "$scope",
            function ($scope) {
               $scope.val1 = 0;
               $scope.val2 = 0;
               $scope.check = function () {
                  if (angular.equals($scope.val1, $scope.val2))
$scope.msg = "Input values are equal.";
                  else $scope.msg = "Input values are not equal.";
               };
            },
         ]);
      </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.

Example − Check if the references are equal or not

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

<!DOCTYPE html>
<html>
   <head>
      <title>angular.equals()</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>
      <div ng-controller="demo">
         Password: <br />
         <input type="password" ng-model="pass" />
         <br />
         <br />
         Confirm Password: <br />
         <input type="password" ng-model="PASS" ng-change="match()"/><br />
         <p ng-show="isMatch" style="color: green;">Password matched</p>
         <p ng-hide="isMatch || PASS==null" style="color: red;">
            Password does not match
         </p>
      </div>
      <!-- Script for passing the values and checking... -->
      <script>
         var app = angular.module("app", []);
         app.controller("demo", [
            "$scope",
            function ($scope) {
               $scope.match = function () {
                  $scope.isMatch = angular.equals($scope.pass, $scope.PASS);
               };
            },
         ]);
      </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.

raja
Published on 08-Oct-2021 12:34:24
Advertisements