简体   繁体   English

AngularJS NgIf或NgHide(如果另一个元素不存在或隐藏)

[英]AngularJS NgIf or NgHide if another element is not present or hidden

    <div>
        <ul class="list-inline alert alert-danger col-sm-9">
            <li>
                <h1>Daño Global</h1>
            </li>
            <li ng-repeat="(key, value) in hoy | groupBy: 'central'" ng-if="value.length >= 3" >
                <h1><span class="label label-danger"><% key %></span></h1>
            </li>
        </ul>
    </div>

I have "noquery" taken from an AJAX call and filtered by current date, that info is added to an array, from that point I need to take the Keys from that array so I can Hide or remove the element with ng-hide or ng-if if it has 3 or more element. 我从AJAX调用中获取了“ noquery”并按当前日期进行了过滤,该信息已添加到数组中,从那一点起,我需要从该数组中获取Keys,以便可以使用ng-hide或ng隐藏或删除该元素-如果它具有3个或更多元素。 That's the context of my question. 这就是我的问题的背景。

I have done it but I want to hide or remove the "ul" or the entire DIV if the li object is not in the DOM or if it's hidden. 我已经完成了,但是如果li对象不在DOM中或被隐藏了,我想隐藏或删除“ ul”或整个DIV。

Edit: I use the ng-if in the li to show some info in it, I'll need that if in the li if I have more than 3 elements with the same key shows the key name but if I don't the ul hides or don't load at all. 编辑:我在li中使用ng-if来显示其中的信息,如果在li中如果我有3个以上具有相同键的元素显示键名,则我需要它,但是如果我没有ul隐藏或根本不加载。 Hope I clarify some of the doubts 希望我澄清一些疑问

I'm struggling a little to understand your use case, but I think you're saying that you want to hide the entire list if it would be longer than three items. 我在努力理解您的用例时有些挣扎,但是我认为您是要隐藏整个列表(如果该列表长于三个项目)。 Is that the case? 是这样吗 For example, I don't see what ng-if="value.length >= 3" is checking, if value isn't used in your repeater. 例如,如果中继器中未使用value ,我看不到ng-if="value.length >= 3"正在检查什么。

In any case, my guess is that you want to test for the number of keys in the "hoy" object. 无论如何,我的猜测是您要测试“ hoy”对象中的键数。 You'd do that using Object.keys , like this: 您可以使用Object.keys来做到这一点,就像这样:

<ul class="list-inline alert alert-danger col-sm-9" ng-if="Object.keys(hoy) > 3">

Does that make sense? 那有意义吗? What is it that you're trying to hide? 您要隐藏什么?

David, I think that what you want to do is have a boolean in the the controller that looks at the array and determines whether the array has more than three elements: 大卫,我想您想做的是在控制器中有一个布尔值,该布尔值查看数组并确定数组是否具有三个以上的元素:

var showDiv= array.length > 0;
var showLI = array.length >= 3;

Then, you will want to have another ng-if in the html: 然后,您将需要在HTML中添加另一个ng-if

 <div ng-if="showDiv">
    <ul class="list-inline alert alert-danger col-sm-9">
        <li>
            <h1>Daño Global</h1>
        </li>
        <li ng-repeat="(key, value) in hoy | groupBy: 'central'" ng-if= "showLI">
            <h1><span class="label label-danger"><% key %></span></h1>
        </li>
    </ul>
</div>

(Notice I got rid of the ng-if in the list as it is no longer necessary). (请注意,我不再使用列表中的ng-if ,因为它不再需要)。

I updated the answer to account for what you state below. 我更新了答案,以说明您在下面的内容。

Thanks for the help people, you surely help me to think out of the box and take another aproach, it was so easy, I apologize for the lack of info and clear instructions: 感谢您的帮助,您一定会帮助我跳出思路,采取另一种方式,这很容易,对于缺乏信息和明确的说明,我深表歉意:

My controller: 我的控制器:

$http.get(API_URL + "noqueries").then(function(response) {
        $scope.noqueries = response.data;
        var dHoy = []; //Here I'll add the json data in order to filter it
        angular.forEach($scope.noqueries, function(value) {
        dHoy.push(value);
        });
        dHoy = filterFilter(dHoy, {fecha: $scope.date});
        var centrales = $filter('groupBy')(dHoy, "central"); //filter to a matching date.
        $scope.count = 0; //Count of repeating incidents
        $scope.dglobal = [];

        angular.forEach(centrales, function(value, key) {
        if (value.length >= 3) {
          $scope.count+= 1;//This'll triger the div (I asked you about the ul but with the div I feel more confortable, sorry)
          $scope.dglobal.push(key); //Here I save the name of the Key in order to display it later.
        }
        });
    });

I added the http.get so you can see the call to the json. 我添加了http.get,以便您可以看到对json的调用。

My view: 我的观点:

<div ng-if="count > 0"> //This shows only if I have 3 or more values matchig
    <ul class="list-inline alert alert-danger col-sm-9">
        <li>
            <h1>Daño Global</h1> //This was the cause of my problem, I used it as a tag and didn't want it to show up if the li was empty.
        </li>
        <li ng-repeat="d in dglobal" >
            <h1><span class="label label-danger"><% d %></span></h1>// This shows a list of the keys inline.
        </li>
     </ul>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM