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:
<div ng-app="myApp"> <div ng-controller="myController"> Value 1 <br/> Value 2 <br/> <button ng-click="compareValues()">Compare Values</button> <br/> Result = {{result}} </div> </div> |
If you run the above example it will produce the output something like this –
Compare two Objects in AngularJs Example:
You can compare two objects in angularjs as below –
Compare two Strings in AngularJs Example:
<div ng-app="myApp"> <div ng-controller="myController"> <b>User 1</b> Name <br/> Phone <br/> <b>User 1</b> Name <br/> Phone <br/> <button ng-click="compareObjs()">Compare Values</button> <br/> Result = {{result}} </div> </div> |