当选择下拉列表时,在Angular中有多个字段

时间:2016-02-29 15:20:08

标签: javascript angularjs sorting angularjs-ng-repeat angularjs-orderby

如何使用select元素按多个字段对数组进行排序?

  <select ng-model="selectedOrder">
    <option value='id'>id</option>
    <option value='-id'>-id</option>
    <option value='country'>country</option>
    <option value='address'>address</option>
    <option value='["country","-id"]'>country, -id</option>
    <option value='["-country","address"]'>-country, address</option>        
  </select>

<ul>
  <li ng-repeat="detail in details | filter:{country: countryFilter} | orderBy:selectedOrder">{{ detail }}</li>    
</ul>

如果我在控制器中设置订单选项,一切都很好:
    $scope.selectedOrder = ["country", "-id"];

如果选择选项&#34; country,-id&#34;或者&#34; -country,address&#34;然后排序不会发生。

此处的完整示例http://plnkr.co/edit/w968MMN3qT9AB4XXRosV?p=preview

1 个答案:

答案 0 :(得分:1)

在控制器中指定不同的选项,它将以这种方式顺利运行。我不认为value的{​​{1}}属性可以包含任何类型的对象。

&#13;
&#13;
option
&#13;
var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {


  $scope.options = [{
    label: "id",
    value: "id"
  }, {
    label: "-id",
    value: "-id"
  }, {
    label: "country",
    value: "country"
  }, {
    label: "address",
    value: "address"
  }, {
    label: "country, -id",
    value: ["country", "-id"]
  }, {
    label: "-country, address",
    value: ["-country", "address"]
  }];


  // all countries
  $scope.details = [{
    id: 1,
    country: 'Finland',
    address: 'Mainstreet 2'
  }, {
    id: 2,
    country: 'Mexico',
    address: 'Some address 1'
  }, {
    id: 3,
    country: 'Canada',
    address: 'Snowroad 45'
  }, {
    id: 4,
    country: 'Finland',
    address: 'Rainbow 12'
  }, {
    id: 5,
    country: 'Canada',
    address: 'Beverly 15'
  }, {
    id: 6,
    country: 'Mexico',
    address: 'Some address 3'
  }];
});
&#13;
&#13;
&#13;