AngularJS – 等價(equals)方法


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 檔案一樣執行它。你將在瀏覽器視窗中看到以下輸出內容。

更新於: 2021 年 10 月 8 日

1K+ 檢視

開始你的 職業生涯

完成課程並獲得認證

開始學習
廣告
© . All rights reserved.