AngularJS不对ng-repeat进行排序

时间:2017-09-17 17:20:24

标签: javascript angularjs

AngularJS不对ng-repeat进行排序。 我做了,

<div ng-repeat="list in lists">
<div style="float: left; margin-left: 5px;">
  <div id="tasks" >
    <h3>{{ list.name }}</h3>
    <ul>
      <li ng-repeat="card in cards | orderBy:'card.position'" ng-if="list._id == card.list">{{ card.position }}<button ng-click="take(card.position)">HERE</button>{{ $index }}</li>
      </ul>
      <form ng-submit="addTask(list._id, $index, newTask)">
        <input type="text" ng-model="newTask" placeholder="add a new task" required />
        </form>
  </div>
  </div>
</div>


console.log($scope.cards[0]);

以上代码的输出是:

Object { _id: "59bc0936c84be51d70f786e7", name: "first", list: "59bbdeae1ebcd215a4b7af62", position: 7200, __v: 0, created: "2017-09-15T17:09:10.813Z", updated: "2017-09-15T17:09:10.813Z" }

https://imgur.com/a/3BL2e

我在哪里犯了错误?

1 个答案:

答案 0 :(得分:5)

应该只是 orderBy:'position'

<li ng-repeat="card in cards | orderBy:'position'" ng-if="list._id == card.list">{{ card.position }}<button ng-click="take(card.position)">HERE</button>{{ $index }}</li>

<强> EDIT

如果你想按多个字段排序,请把它们放在像这样的数组中,

<li ng-repeat="card in cards | orderBy:['position','list']" ng-if="list._id == card.list">{{ card.position }}<button ng-click="take(card.position)">HERE</button>{{ $index }}</li>