我有国家的地图(双向)。其中由stateName和stateCode组成。
它由以下HTML代码填充,但不能按升序按stateCode或stateName进行过滤。

<div class="col-sm-2">
              <select ng-model="location.stateCode"  ng-change="loadDistricts();" ng-options="stateName for (stateCode, stateName) in stateOptions | orderBy:'stateCode'" class="form-control" id="state"></select>
</div>

//Json Object
    stateOptions = {'MH':'Maharashtra','GJ':'Gujarat','MP':'Madhya Pradesh'};

最佳答案

您颠倒了stateCodestateName

ng-options="stateName for (stateCode, stateName) in stateOptions"


另外,由于stateOptions是一个对象,因此可能会按字母顺序插入键,具体取决于js虚拟机。 orderBy过滤器在这里毫无意义,因为(来自documentation):


  通过表达式谓词对指定数组进行排序。字符串按字母顺序排列,数字按数字顺序排列


fiddle



编辑:如果您想确定地控制顺序,请将您的结构重新整理为数组:

$scope.stateOptions = [{
    stateCode: 'MH',
    stateName:'Maharashtra'
},{
    stateCode: 'GJ',
    stateName:'Gujarat'
},{
    stateCode: 'MP',
    stateName:'Madhya Pradesh'
}];

ng-options="state.stateName for state in stateOptions | orderBy: 'stateCode'"


updated fiddle

10-07 17:33