1

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>
2
  • 1
    why you are using a string false if its a boolean value just leave as ng-show = "matches" no need of $scope in HTML Commented Nov 16, 2016 at 12:18
  • Even if you have googled it, you will surely get better code to implement it. Commented Nov 16, 2016 at 12:28

5 Answers 5

3

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>
Sign up to request clarification or add additional context in comments.

2 Comments

ng-show="matches" and ng-show="!matches" mayhaps
@Mackan That is how I would write it, didn't want to alter his code too much.. I will add in a third snippet :)
1

you are missing one = in the first ng-show and also you don't need to refence the $scope, should be:

ng-show ="matches=='false'"

Comments

1

Everything seems fine except $scope is not used in html file.

<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>

replace $scope.matches with matches only.

"ng-show" or "ng-if" both could be used for this.

Comments

1

You don't need to define comparison operation near ng-show, you can even just do like :

<div align="center" ng-show ="$scope.matches"> // Boolean

Comments

0
.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 here";
       }).error(function(error){
         console.log("Error here");
         $scope.matches="Error here"};
})

Then on the HTML file you can write:

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

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

All you had to do was loose that $scope in the html this is because HTML page is considered as the scope.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.