无法使用ng-if隐藏角度js中的div

时间:2017-11-13 09:01:03

标签: html angularjs

无法在加载页面上隐藏div。 如果数组的值为空,我想显示div。

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

3 个答案:

答案 0 :(得分:1)

这是你应该做的。

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

使用ng-show和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只是将元素的显示属性设置为&#34; none&#34;而ng-if从dom中删除元素

答案 1 :(得分:0)

这可以帮助您 - 根据您的情况使用&&|| -

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

答案 2 :(得分:0)

这可能适合你

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