Angular JS orderBy不使用下拉列表

时间:2014-07-18 09:38:57

标签: angularjs

我正在尝试orderBy使用dropdown值,但它不起作用:(

CODE:

var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
    $scope.datas = [{
        "id": "1",
            "name": "name area 1"
    }, {
        "id": "2",
            "name": "name area 2"
    }, {
        "id": "3",
            "name": "name area 3"
    }, {
        "id": "4",
            "name": "name area 4"
    }];
    $scope.dropdown = [{
        "title": "Newest first",
            "value": "true"
    }, {
        "title": "Oldest first",
            "value": "reverse"
    }];
});

HTML:

    <div ng-app="myApp">
      <div ng-controller="myCtrl">
          <ul ng-repeat="rows in datas | orderBy: 'id':orderByData">
              <li>{{rows.id}} : {{rows.name}}</li>
          </ul>
          <select ng-model="orderByData" ng-options="x.value as x.title for x in dropdown"></select>
      </div>
    </div>

DEOM JS BIN

2 个答案:

答案 0 :(得分:2)

为什么使用反向? 请使用false而不是反向,它应该可以正常工作。

$scope.dropdown = [{
        "title": "Newest first",
            "value": "true"
    }, {
        "title": "Oldest first",
            "value": "false"
    }];

答案 1 :(得分:1)

此处不需要reverse参数。相反,你可以这样做:

<ul ng-repeat="rows in datas | orderBy: getSort()">
    <li>{{rows.id}} : {{rows.name}}</li>
</ul>

getSort定义为:

$scope.getSort = function() {
    return $scope.orderByData ? 'id' : '-id';
};

基本上,您希望排序部件看起来像orderBy: 'id'orderBy: '-id'

演示:http://jsbin.com/pepureta/1/edit?html,js,output