2016-11-24 10 views
0

内のオブジェクトのドロップダウンリストをソートするためにどのように私は持っています。 私が試した:はAngularJS

ng-options="product as product.name for product in vm.products track by product.id" | toArray | orderBy : 'name'" 

をしかし、コンソールに私が取得: TypeError: dbg is undefinedを。 角度を使用してソートするにはどうすればよいですか?

+0

も常に行われてもよい任意のフィルタリングの後にある文で追跡します。 – alphapilgrim

答えて

1

これは正しい方向に出力されるはずです。それが役に立てば幸い!実際の例が含まれていますが、逆の順序で項目を表示する場合は3番目のパラメータが真です。

これは、私があなたがそれに応じて注文するように見えると思います。

ng-options="product as product.name for product in vm.products | toArray | orderBy : 'name' track by product.id" 

function exampleController($scope) { 
 
    $scope.phones = [{ 
 
    model: 'anteater' 
 
    }, { 
 
    model: 'bee' 
 
    }, { 
 
    model: 'cat' 
 
    }]; 
 
} 
 

 
angular 
 
    .module('example', []) 
 
    .controller('exampleController', exampleController);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div class="container-fluid" ng-app="example"> 
 
    <div class="container" ng-controller="exampleController"> 
 
    <div class="row"> 
 
     <ol> 
 
     <li ng-repeat="phone in phones | orderBy: 'model': true track by $index" ng-bind="phone.model"></li> 
 
     </ol> 
 
    </div> 
 
    </div> 
 
</div>

1

これはあなたの場合と同じworking exampleです。

ソートしなければならない次の2つのことがありました。あなたはng-options="product as product.name for product in vm.products | toArray | orderBy : 'name' track by product.id"

よう フィルタはあなたと同じよう以下のworking exampleを見ることができた後track by product.idは終わりにする必要があります。また" | toArray"product as product.name for product in vm.products track by product.id" | toArray | orderBy : 'name'"

  • double quotesを閉じた

    1. 1つの質問です。

      var app = angular.module('app', []); 
       
      
       
      app.controller('TestController', function() { 
       
          this.productDelivered = {}; 
       
          this.products = { 
       
          'product1': { 
       
           id: 4, 
       
           name: 'product B' 
       
          }, 
       
          'product2': { 
       
           id: 3, 
       
           name: 'product D' 
       
          }, 
       
          'product3': { 
       
           id: 1, 
       
           name: 'product A' 
       
          }, 
       
          'product4': { 
       
           id: 2, 
       
           name: 'product C' 
       
          } 
       
          }; 
       
      }); 
       
      
       
      app.filter("toArray", function() { 
       
           return function(input) { 
       
            if(!input) return; 
       
      
       
            if (input instanceof Array) { 
       
             return input; 
       
            } 
       
      
       
            return $.map(input, function(val) { 
       
             return val; 
       
            }); 
       
           }; 
       
      }); 
       
      
       
      angular.bootstrap(document, ['app']);
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
       
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
       
      <div ng-controller="TestController as vm"> 
       
      <select class="form-control" id="field_productDelivered" name="productDelivered" ng-model="vm.productDelivered.productDelivered" 
       
           ng-options="product as product.name for product in vm.products | toArray | orderBy : 'name' track by product.id"> 
       
          <option value=""></option> 
       
      </select> 
       
      </div>