angular-equal-3

 

In the example, we have two drop-down list and both drop-down list bind with same object array, angular.equals check if both drop-down list have same selected item then it will return true otherwise false. and it will display different messages based on condition. See the code snippet:

 
 
 
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome in the AngularJS</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script> </head> <body ng-app="app"> <div ng-controller="equalsController"> <select ng-model="studentId1" ng-options="student.name as student.name for student in students" ng-change="isEqual()"></select> <select ng-model="studentId2" ng-options="student.name as student.name for student in students" ng-change="isEqual()"></select> <br /> {{msg}} </div> </body> </html> <script> var app = angular.module("app", []); app.controller('equalsController', ['$scope', function ($scope) { $scope.students = [{ name: 'Herry', id: 1 }, { name: 'John', id: 2 }, { name: 'Peter', id: 3 }]; $scope.isEqual = function () { $scope.msg = angular.equals($scope.studentId1, $scope.studentId2) == true ? "Selected item is same" : "Selected item is not same" } }]); </script>
Output