角度选择绑定字典

angular select bind dictionary

本文关键字:字典 绑定 选择      更新时间:2024-02-06

我尝试将的选项绑定到字典:

<select ng-model="vm.incident.State" ng-options="key for (key,value) in vm.stateOptionSet" style="width: 250px;">

就目前而言,一切都很好,但当我使用这个时:

<select ng-model="vm.incident.State" ng-options="key as value for (key,value) in vm.stateOptionSet" style="width: 250px;">

选择框没有选择适合ng模型的条目。

有人能告诉我怎么了吗?

您的两个示例都在工作。看看我的例子:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="" ng-controller="customersController">
  <select ng-model="selection" ng-options="key for (key,value) in optionSet" style="width: 250px;"></select>
  <select ng-model="selection" ng-options="key as value for (key,value) in optionSet" style="width: 250px;"></select>
  <br />Selection: {{selection}}
</div>
<script>
  function customersController($scope) {
    $scope.selection = {};
    $scope.optionSet = {
      key1: "value1",
      key2: "value2"
    };
  }
</script>

也许您忘记了必须关闭您的选择标签。

来自文档:

使用select as将把select表达式的结果绑定到模型,但<select><option> html元素的值将是集合中值的索引(对于数组数据源)或属性名。如果使用track by表达式,则该表达式的结果将设置为选项的值,并选择元素。