简体   繁体   English

有条件的打开/关闭标签显示

[英]Conditional open/close tag display

I'm wondering how to implement a task (which is very easy in server-side templating) in angular like: 我想知道如何以类似的角度实现任务(在服务器端模板中非常容易):

{{ if condition }}
    <div class="container">
{{ endif }}

    <div class="child"></div>

{{ if condition }}
    </div> <!-- closing container -->
{{ endif }}

I'm of course aware of ng-hide, ng-show and ng-hide... but it think I can't use these directives to implement my task... so what should I do? 我当然知道ng-hide,ng-show和ng-hide ...,但是它认为我无法使用这些指令来实现我的任务...所以我该怎么办?

ps: I can't do the following: ps:我不能做以下事情:

<div class="container" ng-if="condition">
    <div class="child"></div>
</div>
<div class="child" ng-if="!condition"></div>

because I'm using the module operator (%) in order to wrap "child" nodes into "container" every X element (where X is a dynamic parameter) 因为我使用模块运算符(%)以便将每个X元素(其中X是动态参数)将“子”节点包装到“容器”中

the final result would be (supposing X is 4): 最终结果将是(假设X为4):

<div class="container">
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
</div>
<div class="container">
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
</div>

... and so on (I'm in a ng-repeat) ...依此类推(我在ng-repeat中)

Group by filter in the template : 按模板中的过滤器分组:

The key part of the solution is that we are computing a property containerId , which depends on the container size and on the index of the element in the array : 解决方案的关键部分是我们正在计算一个属性containerId ,该属性取决于容器的大小和数组中元素的索引:

$scope.arrayItems.forEach(function(item, index) {
    item.containerId = Math.floor(index / size);
});

Then in the template we group the elements by the property containerId : 然后在模板中,按属性containerId将元素分组:

<div class="container" ng-repeat="containers in arrayItems | groupBy: 'containerId'">
    <div class="child" ng-repeat="item in containers">{{item.name}}</div>
</div>

That groupBy filter in the template comes from angular-filter 模板中的groupBy过滤器来自angular-filter

See fiddle 小提琴


Group by in the controller: 在控制器中分组:

In case you don't want to add angular-filter as a dependency, you could group the elements in the controller. 如果您不想添加angular-filter作为依赖项,则可以在控制器中对元素进行分组。 The following example makes use of the groupBy function of lodash : 下面的例子使用了的groupBy的功能lodash

In the controller : 在控制器中:

$scope.itemsGroupedByContainer = _.groupBy($scope.arrayItems, 'containerId');

In the template : 在模板中:

<div class="container" ng-repeat="containers in itemsGroupedByContainer">
    <div class="child" ng-repeat="item in containers">{{item.name}}</div>
</div>

See fiddle 小提琴

I think that you need the ng-repeat directive. 我认为您需要ng-repeat指令。 Try something like this: 尝试这样的事情:

<div ng-repeat="item1 in repeat1 track by $index">

    <div ng-repeat="item2 in repeat2 track by $index"></div>

</div>

Docs: https://docs.angularjs.org/api/ng/directive/ngRepeat 文件: https//docs.angularjs.org/api/ng/directive/ngRepeat

You don't need an if block to add a closing </div> 您不需要if块来添加结束符</div>

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

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