1

I am using angular.js for font-end and node.js for server side.

Now, I am having some list of values(months) in array randomly.

HTML code :

    <div ng-app="">
      <div ng-controller="Ctrl">
        <select ng-model="month">
            <option ng-repeat="month in months">{{month.value}}</option>
      </select>
      </div>
    </div>

Controller code :

function Ctrl($scope) {
     $scope.months = [{"value":"February"},{"value":"April"},{"value":"January"}];
     $scope.month=null;
}

Expected output:

January
February
April

I want in the above order of month in drop-down

siva
  • 593
  • 3
  • 8
  • 19
  • Not sure if possible, but for these kind of things in .Net I usually use a structure with int value, and string description. Maybe this helps... – Mario Garcia Mar 30 '16 at 15:21
  • Try this method to convert from string to number http://stackoverflow.com/questions/10181538/converting-month-name-to-month-number-using-javascript and then add orderBy to the ng-repeat – disperse Mar 30 '16 at 15:25
  • Thanks for your comments Mario Garcia and disperse, l will try in different way. – siva Mar 30 '16 at 15:35
  • Maybe this is a stupid comment, but can you just oder `$scope.months` in the controller code? – mgilson Mar 30 '16 at 15:37
  • @mgilson $scope.months got value from DB randomly ,you mean arrange the order as hard code in controller. – siva Mar 30 '16 at 15:41

1 Answers1

1

you can create a function in your controller like this

 $scope.getMonthValue = function(mon) {
   return new Date(Date.parse(mon.value +" 1, 2000")).getMonth()+1;
 }

which will convert the month name to a date and return the actual month number that the orderBy filter can use to sort.

In the html you can add the orderBy filter:

  <select ng-model="month">
        <option ng-repeat="month in months | orderBy: getMonthValue">{{month.value}}</option>
  </select>
Michael
  • 653
  • 6
  • 19