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

时间:2014-09-03 21:47:31

标签: javascript angularjs angularjs-ng-repeat ng-repeat

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

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

我想在选择框中显示'Architect'的值。但是,在尝试执行ng-repeat时,单引号会让我失望。

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

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

Here is a sample plunker:

3 个答案:

答案 0 :(得分:6)

转义引号How to properly escape quotes inside html attributes?

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

http://plnkr.co/edit/6xUD3Zg0jxV05b41f2Gw?p=preview

答案 1 :(得分:1)

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

答案 2 :(得分:0)

这是使用外部json进行ng重复的完整代码

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

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"
    }
]