AngularJS – equals() 方法

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

AngularJS 中的 equals() 方法主要用于检查两个对象或两个值是否相等。此方法支持值类型、正则表达式、数组和对象。如果函数内部传递的引用对象相等,则返回 True,否则返回 False。

语法

angular.equals(value1, value2)

示例 − 检查引用对象是否相等

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

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

输出

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

示例 − 检查引用是否相等

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

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

输出

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


相关文章