0

unable to hide div on loading page. I want to show the div if the value of array is empty.

    <div ng-hide="loading"  ng-if="users== null || users.length === 0" >
        <h3>No Result Found</h3>
    </div>
8
  • remove ng-hide and check. Commented Nov 13, 2017 at 9:02
  • 1
    combine expressions together: loading && (users==null || users.length===0) Commented Nov 13, 2017 at 9:03
  • use one of the directive ng-hide or ng-if according to your condition Commented Nov 13, 2017 at 9:09
  • @everyone who is saying don't use ng-hide with ng-if can you explain why? Commented Nov 13, 2017 at 9:13
  • 2
    @jitender simply pointless, ng-if removes DOM, ng-hide sets its display to none. Why bother hiding it, when DOM will be removed? Commented Nov 13, 2017 at 9:21

3 Answers 3

2

This is what you should do.

//This will check if loading is false and Users are not undefined || less than equal to 0 (empty array)
<div ng-if="!loading || !users || users.length <= 0">
    <h3>No Result Found</h3>
</div>

//Show data if users are defined and length is greater than 0
<div ng-if="users && users.length > 0">
        <h3>Data</h3>
</div>

Using ng-show and ng-hide

//Show if loading is true or users are undefined or users are empty
//Hide if loading is false or users are defined or users are not empty
<div ng-show="loading || !users || users.length <= 0"
     ng-hide="!loading || users || users.length > 0">
        <h3>No Result Found</h3>
</div>

//Show if users are defined and length is greater than 0
//Hide if loading is true or users are undefined or users are empty
<div ng-show="users && users.length > 0"
     ng-hide="loading || !users || users.length <= 0">
        <h3>Data</h3>
</div>

ng-hide/show just sets the display property of the element to "none" whereas, ng-if removes the element from the dom

Sign up to request clarification or add additional context in comments.

13 Comments

not working. there is two div one is for to display result and one is for no result.. unable to understand where to use ng-if
@user3074158 check my answer now. I have updated it and explained everything.
where is define the scope of loading ?
That you will be doing it in your controller. As you haven't given us your code so I am assuming that "loading" is a $scope variable and defined already somewhere in your code just like users array.
In controller, i mentioned this two lines only $scope.users = []; $scope.loading = [] ;
|
0

This may work for you

<div ng-if="!loading && (typeof(users) === 'undefined'  || users== null || users.length === 0)" >
    <h3>No Result Found</h3>
</div>

3 Comments

not working. there is two div one is for to display result and one is for no result.. unable to understand where to use ng-if
I think you should check value of $scope.loading. Why not upload whole code on plunker?
<div class="container" ng-hide="" ng-repeat="user in users" > here m displaying result </div> <div ng-if="!user || user.length <= 0"> <h3>No Result Found</h3> </div>
0

This may help you - use && or || as per your case here -

<div ng-hide="loading && users.length == 0">
    <h3>No Result Found</h3>
</div>

6 Comments

Note that Boolean([]) === true, so if users is a defined array, the above expression may be truthy even if the array is empty.
Yeah, that's what he wants. ng-hide will hide the div if users array isn't empty.
Again, an empty array in JS evaluates as boolean true, unlike in PHP for example. So the div would be hidden as long as the array is defined, regardless if its empty or not. Simply try it.
okay, So, here, we update it with - users.length. Thank You!
not working. there is two div one is for to display result and one is for no result.. unable to understand where to use ng-if
|

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.