【问题标题】:AngularJS ngRepeat orderBy in ChromeChrome中的AngularJS ngRepeat orderBy
【发布时间】:2015-10-30 18:55:31
【问题描述】:

我有一个简单的应用程序,可以在 Angular 中显示多行数据。当控制器最初加载数据时,我希望行以它们添加到我的 rows 数组的相同顺序出现。因此,orderBy 属性最初将设置为空字符串。单击列标题应将orderBy 属性设置为适当的值。

这是我的小提琴:http://jsfiddle.net/fLjMR/3/

这是我的 JS:

function ctrl($scope)
{
    var rows = [];
    for(var i = 10;i < 30;i++)
    {
        rows.push({name: "Fake Name " + i, email: "fakeemail" + i + "@gmail.com"});
    }

    $scope.rows = rows;
    $scope.orderBy = "";
};

这是我的 HTML:

<table ng-app ng-controller="ctrl">
    <thead>
        <th><a ng-click="orderBy='name'">Name</a></th>
        <th><a ng-click="orderBy='email'">Email</a></th>
    </thead>
    <tbody>
        <tr ng-repeat="row in rows | orderBy:[orderBy]">
            <td>{{row.name}}</td>
            <td>{{row.email}}</td>
        </tr>
    </tbody>
</table>

当我在 IE 和 FF 中执行此操作时,行最初按添加顺序显示,但当我在 Chrome 中执行此操作时,中间项(在本例中为第 20 行)出现在列表的开头。这是为什么呢?

【问题讨论】:

    标签: angularjs google-chrome angularjs-ng-repeat


    【解决方案1】:

    如果您只有一个要订购的属性,那么您可以只将字符串传递给过滤器而不是数组:

    <tr ng-repeat="row in rows | orderBy:orderBy">
    

    【讨论】:

    • 嗯。那么你知道什么,这解决了它。我知道你可以去掉括号,但我打算稍后添加一些额外的参数。为什么括号会有所不同?
    • 刚刚看了一眼角度源。如果排序谓词(冒号后面的东西)是假值,orderByFilter 会短路并返回未排序的数组。空字符串是假值。包含空字符串的数组不是假值,因此尝试使用该空字符串作为谓词进行排序。
    • 好东西。我想你搞定了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2014-06-01
    • 2016-08-25
    • 2017-05-13
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    相关资源
    最近更新 更多