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相关的内容: