如何在ng-repeat中显示键值选项

时间:2018-05-04 08:44:23

标签: javascript angular

我的对象以这种格式返回:

{
  "1":
    {"130":"Option1"},
  "2":
    {"133":"Option4"},
  "3":
    {"131":"Option3"},
  "6":
    {"132":"Option5"}
}

我想显示例如的值。选择下拉列表中的{"132":"Option5"} 132 为值, Option5 为角色2中的文字

我试过了:

<option ng-repeat="(key, value) in modeltest.fieldOptions"
  value="{{key}}">
  [[value]]
</option>`

目前正在以图片显示打印。enter image description here

1 个答案:

答案 0 :(得分:2)

You can create a new function in your controller as keyValue() that will take the object and return the value as Object.keys() do not work directly in an Angularjs expression:

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.modeltest = {
    "fieldOptions" : {
    "1":
    {"130":"Option1"},
    "2":
        {"133":"Option4"},
    "3":
        {"131":"Option3"},
    "6":
        {"132":"Option5"}
      }
   };
   $scope.keyValue = function(obj){
     return obj[Object.keys(obj)[0]];
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
<option ng-repeat="(key, value) in modeltest.fieldOptions"
            value="{{keyValue(value)}}">
       {{keyValue(value)}}
</option>