ng-repeat按特定顺序显示html

时间:2014-05-01 12:16:49

标签: javascript html json angularjs

我收到了json消息:

我正在尝试按特定顺序将json消息加载到html中。

您将在json消息中找到位置值,该数组中的第一个元素:0表示订单。

有时,json消息乱序,不像示例。

有没有办法按顺序输出html中的json消息:0,1,2,3。

HTML部分是:

<div class="container">
            <div ng-repeat="question in questions">
                <div class="row">
                    // load the question in increase order
                </div>
            </div>
        </div>

Json消息:

[ { "Question" : { "description" : { "default" : "hello" },
        "options" : [ { "value" : {  } } ],
        "title" : { "default" : "hello world" }
      },
    "Selection" : "text",
    "position" : [ 0,
        12,
        0
      ]
  },
  { "Question" : { "description" : { "default" : "hello 2" },
        "options" : [ { "value" : {  } } ],
        "title" : { "default" : "hello 2" }
      },
    "Selection" : "paragraph",
    "position" : [ 1,
        12,
        0
      ]
  },
  { "Question" : { "description" : { "default" : "hello 3" },
        "options" : [ { "value" : { "default" : "1" } },
            { "value" : { "default" : "2" } }
          ],
        "title" : { "default" : "hello 3" }
      },
    "Selection" : "radio",
    "position" : [ 2,
        12,
        0
      ]
  },
  { "Question" : { "description" : { "default" : "hello 4" },
        "options" : [ { "value" : { "default" : "check11" } },
            { "value" : { "default" : "check22" } }
          ],
        "title" : { "default" : "hello 4" }
      },
    "Selection" : "checkbox",
    "position" : [ 3,
        12,
        0
      ]
  },
  { "Question" : { "description" : { "default" : "hello 5" },
        "options" : [ { "value" : { "default" : "list1" } },
            { "value" : { "default" : "list2" } }
          ],
        "title" : { "default" : "hello 5" }
      },
    "Selection" : "list",
    "position" : [ 4,
        12,
        0
      ]
  }
]

2 个答案:

答案 0 :(得分:3)

您需要将位置数组中的第一项指定为orderBy过滤器:

ng-repeat="question in questions | orderBy:'position[0]'"

Fiddle

答案 1 :(得分:0)

尝试使用orderBy

<tr ng-repeat="question in questions | orderBy:<function>">
  ...      
</tr>