【问题标题】:ng-options: How to access to position (index) in an object which has no index in key or valueng-options:如何访问在键或值中没有索引的对象中的位置(索引)
【发布时间】:2016-09-18 14:57:08
【问题描述】:

我有一个 JSON 对象。如下:

[
   {"Title":"x", "Type":"y", "Desc":"First Description"},
   {"Title":"r", "Type":"q", "Desc":"Second Description"}
]

我想在这个 obj 中有一个包含标题的下拉菜单(使用 ng-options)。然后,我想在对象中使用所选标​​题的索引。 (想象一下该对象将用作数组(称为 myArray),正如 user194715 回答的 here
例如:

var type = myArray[selectedIndex].Type;
var description= myArray[selectedIndex].Desc;

在这种情况下如何访问索引?

【问题讨论】:

    标签: javascript angularjs json indexing angularjs-ng-options


    【解决方案1】:

    这样试试

    var app = angular.module("app",  []);
    
    app.controller('mainCtrl', function($scope){
      $scope.myArray =
            [
              {"Title":"x", "Type":"y", "Desc":"First Description"},
              {"Title":"r", "Type":"q", "Desc":"Second Description"}
            ];
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    <div ng-app="app" ng-controller="mainCtrl">
        <select ng-model="select" ng-options="key as value.Title for (key,value) in myArray">
       </select>
     <span>{{myArray[select].Type}}</span>
     <span>{{myArray[select].Desc}}</span>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多