AngularJS如何使用angular.equals()函数?示例

angularJS中的angular.equals()函数用于比较两个对象或两个值,无论它们是否相同。如果两个值相同,则返回TRUE,否则返回FALSE。

语法如下:

angular.equals(val1, val2)

其中val1和val2是要比较的值或对象。

返回值:返回TRUE或FALSE

范例1:

<html>
<head>
  <script src =
  "//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
  </script>
  <title>angular.equals()</title>
  </head>
  
<body ng-app = "app" style = "text-align:Center">
   <h1 style = "color:green">lsbin</h1>
   <h2>angular.equals()</h2>
  
<div ng-controller = "geek">
   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>
   var app = angular.module("app", []);
   app.controller('geek', ['$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>

输出如下:

输入之前:

输入后:

范例2:

<html>
<head>
  <script src =
  "//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
  </script>
  <title>angular.equals()</title>
  </head>
  
  <body ng-app = "app" style = "text-align:Center">
    <h1 style = "color:green">lsbin</h1>
    <h2>angular.equals()</h2>
  
<body ng-app = "app">
  <div ng-controller = "geek">
     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>
   var app = angular.module("app", []);
   app.controller('geek', ['$scope', function ($scope) {
    $scope.match = function () {
    $scope.isMatch = angular.equals($scope.pass, $scope.PASS);
}
}]);
     
</script>
</body>
</html>

输出如下:

原来:

输入错误:

正确输入:

更多前端开发相关内容请参考:lsbin - IT开发技术https://www.lsbin.com/

查看以下更多angularJS相关的内容:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值