I am facing an issue with validating my input against JSON data. Every time I try to compare it with the JSON, only the else block gets executed. Can someone please help me resolve this problem?
<body ng-app="fileGetting" ng-controller="loadFile">
<label>Firstname:</label><input type="text" ng-model="placeFile.fname"><br>
<label>Lastname:</label><input type="text" ng-model="placeFile.lname"><br>
<button ng-click="fun()">Submit</button><br>
<div ng-repeat="x in placeFile">
<p>{{x.fname}}</p>
</div>
<script>
angular.module("fileGetting", [])
.controller("loadFile", function($scope, $http) {
$http.get("exam.json").then(function(response) {
$scope.placeFile = response.data.names;
var x = $scope.placeFile;
$scope.fun = function() {
angular.forEach(x, function(value, key) {
if ($scope.placeFile.fname == x.key && $scope.placeFile.lname == x.key)
{
alert("hi ram");
}
else
{
alert("this is incorrect");
}
});
}
});
});
</script>
Below is the JSON data being used:
{
"names":[
{
"fname":"Ram",
"lname":"Chandru"
},
{
"fname":"Chandran",
"lname":"Krishna"
},
{
"fname":"Jayanth",
"lname":"Jo"
}
]
}