Javascript ng-repeat:用数组填充下拉选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25654187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:39:28  来源:igfitidea点击:

ng-repeat: populate drop down options with array

javascriptangularjsangularjs-ng-repeatng-repeat

提问by Josue Espinosa

I have a simple JavaScript object that looks like this:

我有一个简单的 JavaScript 对象,如下所示:

$scope.obj = { "'Architect'": ["asdf","d","e","y"]};

I'd like to show the values of 'Architect'in a select box. However, the single quotes are throwing me off when trying to do the ng-repeat.

我想'Architect'在选择框中显示 的值。但是,在尝试执行ng-repeat.

<select>
    <option ng-repeat="row in obj['Architect']" value="{{row}}">{{row}}</option>
</select>

That does not populate the select box, it just shows an empty select box. I assume it is interpreting the single quotes as a string literal, but even if I add single quotes and escape them, it still doesn't work as expected. Am I missing something?

这不会填充选择框,它只显示一个空的选择框。我假设它将单引号解释为字符串文字,但即使我添加单引号并转义它们,它仍然无法按预期工作。我错过了什么吗?

Here is a sample plunker:

这是一个示例 plunker:

回答by Professor Allman

回答by Emech

why don't you use "ng-options" for select? take a lock at this AngularJs API: select

为什么不使用“ng-options”进行选择?锁定这个 AngularJs API:选择

回答by ankush

Here is the complete code for ng repeat with external json

这是 ng repeat with external json 的完整代码

HTML

HTML

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>datatable using jquery.datatable in angularjs</title> 
  <link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'>
<link rel='stylesheet prefetch' href='https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css'>
      <link rel="stylesheet" href="css/style.css"> 
</head>
<body>
  <div class="container" ng-app="problemApp" data-ng-controller="validationCtrl">
  <select>
    <option ng-repeat="item in testdata" value="">{{item.name}}</option>
</select>
</div>
  <script src='https://code.jquery.com/jquery-2.2.4.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js'></script>
<script  src="js/index.js"></script>
</body>
</html>

index.js

索引.js

var app=angular.module('problemApp', []);
app.controller('validationCtrl',function($scope,$http){
    $http.get('http://localhost/Dtable_angular/ngrepeatdropdown/test.json').success(function (data) {

                $scope.testdata = data;
                console.log($scope.testdata)
        })


$scope.dataTableOpt = {
   //custom datatable options 
  // or load data through ajax call also
  "aLengthMenu": [[10, 50, 100,-1], [10, 50, 100,'All']],
  };
});

test.json

测试文件

[{
        "countryId": 1,
        "name": "France - Mainland",
        "desc": "some description"
    },
    {
        "countryId": 2,
        "name": "Gibraltar",
        "desc": "some description"
    },
    {
        "countryId": 3,
        "name": "Malta",
        "desc": "some description"
    }
]