June June - 2 months ago 6
AngularJS Question

Angular hide or show a button if the content of the field in table contains a specific text

In my html

<div ng-app="myApp" ng-controller="customersCtrl">
<table border="1">
<tr ng-repeat="x in names">
<td>{{x.Name}}</td>
<td>{{x.City}}</td>
<td>{{x.Country}}</td></tr>
</table>
</div>
<a href="#" >Show or Hide Button</a>


In my controller

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope) {
$scope.names =[
{
"Name" : "Max Joe",
"City" : "Lulea",
"Country" : "Sweden"
},
{
"Name" : "Manish",
"City" : "Delhi",
"Country" : "India"
}
];
});


I need to show the button if there are no records or 1 of the country is India. I'm working on this but no avail http://jsfiddle.net/junedc/n8ejgtwa/1/ Thanks for the help guys really appreciated.

Answer

Use an array filter as well as array length to set a boolean

var hasIndia = $scope.names.filter(function(item){
    return item.Country === 'India'
}).length;

$scope.showButton = !$scope.names.length || hasIndia;

In view:

<a ng-show="showButton" href="#" >Show or Hide Button</a>