AngularJS, order text selection with a non-array model? -
this question has answer here:
- angularjs sorting property 10 answers
i using ng-options="ref display (ref, display) in
syntax display non-array object dropdown, having trouble ordering dropdown values.
how should use orderby
filter sort dropdown display
value?
<div ng-controller="myctrl"> <select name="category" type="text" ng-model="categories " ng-options="ref display (ref, display) in categories | orderby:'display'" > </div> var myapp = angular.module('myapp',[]); function myctrl($scope) { $scope.categories = { cat2: "category 2", default: "anone", zone: "zone", }; }
orderby can't applied plain object, alternatively can define method in controller convert object array
demo
var myapp = angular.module('myapp',[]); myapp.controller('myctrl', function($scope) { $scope.categories = { cat2: "category 2", default: "anone", zone: "zone", }; $scope.templatesary = function() { var ary = []; angular.foreach($scope.categories, function(val, key) { ary.push({ "category": val }); }); return ary; }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myapp" ng-controller="myctrl"> <select class="form-control" id="selection" ng-model="currentselected" ng-options="selection.category selection.category selection in templatesary() | orderby:'category'"></select> </div>
Comments
Post a Comment