Ng-switch在ng-repeat问题中

Ng-switch in ng-repeat issue

本文关键字:问题 ng-repeat Ng-switch      更新时间:2023-09-26

我有一个奇怪的问题,让我发疯

我基本上有一个不同类型的文章列表:新闻,推文和视频

我这样渲染它们:

<div ng-repeat="item in items | orderBy:-timestamp track by item.id" ng-switch="item.type">
        <?php  
             include("templates/ang-youtube.html");  
             include("templates/ang-tweet.html");
             include("templates/ang-news.html");
        ?>
</div>

在每个include中,我做了这样的事情:

<div class="item" masonry-brick ng-switch-when="news">
...content in here
</div>

<div class="item" masonry-brick ng-switch-when="tweet">
...content in here
</div>

<div class="item" masonry-brick ng-switch-when="youtube">
...content in here
</div>

现在的问题是youtube项目总是首先呈现。条目的顺序(时间戳)被忽略。tweets和news item的渲染是正确的。

如果我删除开关,只是列出项目作为文本,那么一切都在正确的顺序。只要我添加了开关,youtube项目就会再次首先渲染。

你知道我做错了什么吗?

我已经试过像这样移动ng中继器内部的开关:

<div ng-repeat="item in items | orderBy:-timestamp track by item.id">
    <div ng-switch="item.type">
            <?php  
                 include("templates/ang-youtube.html");  
                 include("templates/ang-tweet.html");
                 include("templates/ang-news.html");
            ?>
    </div>
</div>

但是没有区别。youtube项目总是先渲染。

如果我检查$作用域。

我也尝试过使用ng-if而不是开关,但得到相同的结果

任何想法?: - (

也试过像这样使用ng-include:

<div ng-include="'templates/ang-{{item.type}}.html'"></div>

但是不行

DOH!需要添加保存顺序属性,如下所示

https://github.com/passy/angular-masonry