【问题标题】:How to reorder based on another array in Angular JS如何基于Angular JS中的另一个数组重新排序
【发布时间】:2014-05-28 11:24:36
【问题描述】:

请检查这个小提琴: http://jsfiddle.net/houmaka/vHXy4/

当用户填写不同的字段时,ng-repeat 命令输出。如何根据我的字段数组强制执行自定义排序? (表格有20多个字段,我只是放了一张实际表格的快照。)

快速浏览:

$scope.fields = [];
$scope.fields = {
    contact_person: 'Contact Person',
    app_owner_name: 'Name of Application Owner',
    app_owner_phone_number: 'Application Owner Phone Number',
}

并向他们展示:

  <div>
    <div ng-repeat="(key, value) in user">
        <div ng-show="value">
            <div>{{fields[key]}}:</div>
            <div>{{value}}</div>
        </div>
    </div>
   </div>

谢谢

【问题讨论】:

    标签: javascript jquery angularjs dry


    【解决方案1】:

    我会建议这个字段模型:

    $scope.fields = [
        { index: 3, name: 'Contact Person', id: 'contact_person' },
        { index: 2, name: 'Name of Application Owner', id: 'app_owner_name' },
        { index: 1, name: 'Application Owner Phone Number', id: 'app_owner_phone_number' },
    ];
    

    还有这个 ngRepeat 用法:

        <div ng-repeat="field in fields | orderBy: 'index' ">
            <div ng-show="user[field.id]">
                <div>{{field.name}}</div>
                <div>{{user[field.id]}}</div>
            </div>
        </div>
    

    【讨论】:

    • 谢谢伙计。当我更改索引时,一切正常并且按预期工作。干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 2014-10-25
    • 2017-06-24
    • 2021-07-17
    相关资源
    最近更新 更多