简体   繁体   English

AngularJS创建虚拟指令

[英]AngularJS creating dummy directive

I want to create in AngularJS a "dummy" element to make nested ng-repeats. 我想在AngularJS中创建一个“虚拟”元素来制作嵌套的ng-repeats。
So that following 因此,以下

data.test = [{h:1, d:[11,12]}, {h:2, d:[21,22]}];
----------------------
<dummy ng-repeat="a in data.test">
    <h3>{{a.h}}</h3>
    <p ng-repeat="b in a.d">{{b}}</p>
</dummy>

will expand into 将扩展为

<h3>1</h3>
<p>11</p>
<p>12</p>
<h3>2</h3>
<p>21</p>
<p>22</p>

Note that I don't want to be each group in div or any other element. 请注意,我不想成为div或其他任何元素中的每个组。

I have tried following directive 我已经尝试过以下指令

app.directive('dummy', function () {
    return {
        restrict: 'E',
        replace: true,
        template: ''
    };
});

However, it seems like it is preserving dummy nodes. 但是,似乎正在保留dummy节点。

Is this possible? 这可能吗?

The directives ng-repeat-start and ng-repeat-end can be used for this exact purpose. 指令ng-repeat-startng-repeat-end可用于此确切目的。

<h3 ng-repeat-start="a in data.test">{{a.h}}</h3>
<p ng-repeat-end ng-repeat="b in a.d">{{b}}</p>

without need for a dummy element/directive, as can be seen in this Plunker . 不需要虚拟元素/指令,如本Plunker所示

After searching Google, I found a blog post that answer this. 搜索Google之后,我发现了一个博客文章来回答这个问题。 It shows how to group with ng-repeat-start and ng-repeat-end . 它显示了如何使用ng-repeat-start和ng-repeat-end进行分组 There's a running example with downloadable code. 有一个带有可下载代码的运行示例。 If you jump to the end, you can see the code that looks like this: 如果跳到最后,您将看到如下代码:

<body ng-controller="TeamListCtrl">
    <div ng-repeat-start="team in teams" class="header">{{team.name}}</div>
    <div ng-repeat="player in team.players" class="item">{{player.firstName}} {{player.lastName}}</div>
    <div ng-repeat-end><br /></div>
</body>

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

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