Filter JSON Object to Show / Hide / Update Elements based on Conditions in Angularjs

Introduction:
This article explains how to use AngularJS to filter JSON object to show or hide or update elements with example or AngularJS filter json object in controller with example. By using filter option in ng-repeat we can enable/disable or show/hide required elements from JSON object.

To show or hide values based on JSON object in AngularJS we need to write the code like as shown below

 $scope.checkvalues = function (user) {
return user.id != "5";
}

If you want to check it in complete example you need to write the code like as shown below

HTML Code-

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>angularjs filter json object to show/hide or update elements</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js">
</script>
<script type="text/javascript">
var app = angular.module('sampleapp', [])
app.controller('samplecontrol', function ($scope) {
$scope.sample = [{
id: '1',
name: 'Vepsh Tecck'
}, {
id: '2',
name: 'India'
}, {
id: '3',
name: 'UK'
}, {
id: '4',
name: 'USA'
}, {
id: '5',
name: 'China'
}, {
id: '6',
name: 'Rusia'
}];

$scope.checkvalues = function (user) {
return user.id != "5";
}
});
</script>
</head>
<body data-ng-app="sampleapp" data-ng-controller="samplecontrol">
<form id="form1">
<div ng-repeat="user in sample | filter:checkvalues">
{{user.name}} <br/>
</div>
</form>
</body>
</html>

Output-

If you check below output it will display all values except id = 5

Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

No comments:

Post a Comment