user6579134 user6579134 - 20 days ago 5
AngularJS Question

ng-show if json returns [false] with angularjs

I'm new to angulars and i working on a project where i want a div to only show when the json data returns

[false]
. i sometimes get the json to return
[false]
when there is no results from the query.

JS

.controller('query_ctrl', function($scope, $http){ $http.get('http://localhost/db/templates/search/matches.php').success(function(data){
console.log(JSON.stringify(data));
$scope.matches=data;
});
})


HTML

<div ng-controller="query_ctrl">
<div align="center" ng-show ="$scope.matches='false'">json returned false
</div>

<div align="center" ng-show ="$scope.matches!='false'">json returned true
</div>
</div>

Answer

You don't need to reference $scope in the HTML.

<div ng-controller="query_ctrl">
<div align="center" ng-show ="matches=='false'">json returned false 
 </div>

<div align="center" ng-show ="matches!='false'">json returned true 
 </div>
</div>

Also, as the comment above says - i'm unsure why you're comparing the value to the string 'false', I assume you want to compare to boolean false. See below.

<div ng-controller="query_ctrl">
<div align="center" ng-show="matches==false">json returned false 
 </div>

<div align="center" ng-show ="matches!=false">json returned true 
 </div>
</div>

You could make the ng-show expressions more concise by just having matches as the expression, ng-show will evaluate if it is a truthy value or not, see below.

<div ng-controller="query_ctrl">
    <!-- Will show if matches is a false value -->
    <div align="center" ng-show="!matches">json returned false 
     </div>

    <!-- Will show if matches is a truthy value -->
    <div align="center" ng-show ="matches">json returned true 
     </div>
    </div>