简体   繁体   English

没有 html 元素的 ng-repeat

[英]ng-repeat without an html element

i am trying to generate a blog list but i got a problem with ng-repeat.我正在尝试生成一个博客列表,但我在使用 ng-repeat 时遇到了问题。 my list looks like this我的清单看起来像这样

<ul>
    <li>
        <h2>Title</h2>
        <p>Message</p>
    </li>
    <li>
        <h2>Title</h2>
        <p>Message</p>
    </li>
    <span class="sep2"></sep>
    <li>
        <h2>Title</h2>
        <p>Message</p>
    </li>
    <li>
        <h2>Title</h2>
        <p>Message</p>
    </li>
    <span class="sep2"></sep>
    <li>
        <h2>Title</h2>
        <p>Message</p>
    </li>
    <li>
        <h2>Title</h2>
        <p>Message</p>
    </li>
</ul>

So after every 2 list items, i have a span that levels my next 2 boxes.因此,在每 2 个列表项之后,我就有一个跨度来调整我接下来的 2 个框。 Right now i have this angular code.现在我有这个角度代码。

<ul>
    <li ng-repeat="post in postsJSON">
        <h2>{{post.title}}</h2>
        <p>{{post.message}}</p>
    </li>
</ul>

And i dont know how to generate that span after every second list item.而且我不知道如何在每第二个列表项之后生成该跨度。 Thank you in advance, Daniel!提前谢谢你,丹尼尔!

With angular v1.2 it becomes quite easy, using ng-repeat-start , ng-repeat-end and ng-if , you can check it here : http://jsfiddle.net/DotDotDot/XNJvj/1/使用 angular v1.2 变得非常容易,使用ng-repeat-startng-repeat-endng-if ,你可以在这里查看: http : //jsfiddle.net/DotDotDot/XNJvj/1/

Your code will look like this:您的代码将如下所示:

    <ul>
        <li ng-repeat-start='post in postsJSON'>
            {{post.item}}<br/>
            {{post.message}}
        </li>
        <span ng-if="$odd && !$first" ng-repeat-end>
            <span class="sep2">_____</span>
        </span>
    </ul>

ng-repeat-start/end allows you to enter a loop in a tag and close it in another, in this case, I also added a condition using the $odd parameter of the ng-repeat , showing only every other span ng-repeat-start/end允许您在标签中输入一个循环并在另一个标签中关闭它,在这种情况下,我还使用ng-repeat的 $odd 参数添加了一个条件,仅显示每隔一个范围

The issue here is not really with angular but more with the structure of your markup.这里的问题实际上并不在于 angular,而在于标记的结构。 ul tag should normally only contains li tags as children. ul 标签通常应该只包含 li 标签作为子标签。

To resolve your issue I will stick to the ng-repeat you already have and create the separator with css.为了解决您的问题,我将坚持使用您已有的 ng-repeat 并使用 css 创建分隔符。 Something like that :类似的东西:

<ul class="blog list">
    <li ng-repeat="post in postsJSON" class="blog entry">
        <h2>{{post.title}}</h2>
        <p>{{post.message}}</p>
    </li>
</ul>

CSS : CSS :

.blog.entry {
  border-bottom : 1px solid black // or whatever color
  ...
}

or if you need more control on the separator use css :after and do something like或者,如果您需要对分隔符进行更多控制,请使用 css :after 并执行类似操作

.blog.entry:after {
  content : "";
  ...
 } 

If you are not familiar with :after you can have a look there如果你不熟悉 :after 你可以看看那里

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

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