angular.equals


angular.equals- This is used to check if two objects or two values are equal. It supports the value types, regular expressions, arrays and objects. Here in this tutorial we are going to explain how you can use this function to compare the two values, objects or arrays. You can use our online demo try and edit the code online.


angular.equals | compare strings | Array | Objects | Example

Two values or objects will be equal if one of the following condition is satisfied :

  • If Both values or objects are identical as per === comparison.
  • If Both objects or values are of the same type and all of its properties are equal using angular.equals.
  • If Both values are NaN.
  • If Both values are using the regular expression and they have same representation.

Syntax

Syntax for angular equals is –

AngularJs compare strings | Array | Objects :

angular.equals(object1, object2);

Agruments

  • object1 – Object or value for comparision
  • object2 – Object or value for comparision

Return

This will retrun true or false on the basis of comparision.

angular.equals Example

Let us create a simple example to compare two strings using this function –

Compare two Strings in AngularJs Example:

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
 $scope.result = "";
 $scope.value1 = "";
 $scope.value2 = "";
 $scope.compareValues = function() {
	  $scope.result = angular.equals($scope.value1, $scope.value2);
    };
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
    Value 1 <input type="" ng-model="value1"><br>
	Value 2 <input type="" ng-model="value2"><br>
	
    <button ng-click ="compareValues()">Compare Values</button>
	<br>
	Result = {{result}}
	
</div> 
</div>
</body>  
</html>

Try it »

If you run the above example it will produce the output something like this –

angular.equals | compare strings | Array | Objects | Example

Compare two Objects in AngularJs Example:

You can compare two objects in angularjs as below –

Compare two Strings in AngularJs Example:

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
 $scope.result = "";
 $scope.user1 = "";
 $scope.user2 = "";
 $scope.compareObjs = function() {
	  $scope.result = angular.equals($scope.user1, $scope.user2);
    };
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
    <b>User 1</b>
    Name <input type="" ng-model="user1.name"><br>
	Phone <input type="" ng-model="user1.phone"><br>
	<b>User 1</b>
    Name <input type="" ng-model="user2.name"><br>
	Phone <input type="" ng-model="user3.phone"><br>
	
    <button ng-click ="compareObjs()">Compare Values</button>
	<br>
	Result = {{result}}
	
</div> 
</div>
</body>  
</html>

Try it »


Advertisements

Add Comment

📖 Read More