AngularJS, order text selection with no array model?
I am using the syntax ng-options="ref as display for (ref, display) in
to display a non-array object as a dropdown but I am having trouble ordering the dropdowns.
How do I use a filter orderBy
to sort this dropdown by value display
?
<div ng-controller="MyCtrl">
<select name="category" type="text" ng-model="categories " ng-options="ref as display
for (ref, display) in categories | orderBy:'display'" >
</div>
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.categories = {
cat2: "Category 2",
default: "anone",
zone: "zone",
};
}
+3
source to share
1 answer
Orderby cannot be applied to a plain object, alternatively you can define a method in the controller to convert the object to an 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 as selection.category for selection in templatesAry() | orderBy:'category'"></select>
</div>
0
source to share