不在SELECT中使用ng-options

时间:2014-03-01 07:32:46

标签: angularjs

我正在尝试使用States填充Select中的ng-options。由于我是棱角分明的新手,结果并不像预期的那样。

控制器

$scope.statesList = function () {
        StatesList.query(function (states) {
            $scope.states = states;            
        });
    };

HTML

<td data-ng-init="statesList()">
    <select ng-model="practiceAdd.state" name="state{{$index+1}}" ng-class="{'has-error': editForm.{{$index+1}}.$invalid}" 
        ng-options="state in states" required>
    </select>                                                                
</td>

阵列

$$hashKey: "05S"
_id: "53107e099d985dc404000015"
city: "mumbai"
country: "india"
state: "AR"
street: "1"
zipcode: 42101

1 个答案:

答案 0 :(得分:1)

试试这个......

在HTML中

<select name="countries" id="countries" ng-model="countryCode" ng-options="country.code as country.name for country in countries"></select>

在角度JS代码中

  $scope.countries = [
    {
      name: 'Poland',
      code: 'PL'
    },
    {
      name: 'United Kingdom',
      code: 'UK'
    },
    {
      name: 'United States of America',
      code: 'USA'
    }
  ];
  $scope.countryCode = 'UK';

Working Exaple

在你的例子中;

<td>
    <select ng-model="state.Id" name="state{{$index+1}}" ng-options="state.Id as state.name for state in states" required>
    </select>                                                                
</td>

状态ID和州名应由您定义。