Angularjs无法使用ng-options动态填充下拉列表

时间:2017-01-10 12:20:18

标签: html angularjs

我正在显示一个包含用户信息的表,在此表下方有一个输入框,用户可以输入行索引并根据行索引,使用ng-options动态填充对象。

HTML UI

enter image description here

填充下拉列表后

enter image description here

填充下拉列表后,所有标签都是未定义的。不确定是什么问题。

// html file -

async protected void Page_Load(object sender, EventArgs e)
{
    await TestMethod();
}

// app.js

<html>
<head>
    <title>AngularFilter</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src="../lib/angular.js"></script>
    <script src="js/app.js"></script>
    <link rel="stylesheet" href="styles.css" />
</head>
<body ng-app="filter">
    <div ng-controller="filterController">
        <table>
            <thead>
                <tr>
                    <th>Index</th>
                    <th>Name</th>
                    <th>Date Of Birth</th>
                    <th>Gender</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="employee in data">
                    <th>{{$index + 1}}</th>
                    <th>{{employee.name | lowercase}}</th>
                    <th>{{employee.dob | date:"dd/MM/yyyy"}}</th>
                    <th>{{employee.gender| uppercase}}</th>
                    <th>{{employee.salary | number:2}}</th>
                </tr>
            </tbody>
        </table>
        <br>
        <input type="text" placeholder="Enter the index of array" ng-model="idx" ng-blur="getSelectedIndex()" />
        <select ng-model="element" ng-options="{{optionString}}" >
            <option value="">select option</option>
        </select>
    </div>
</body>

1 个答案:

答案 0 :(得分:1)

您使用object data source $scope.elements作为ng-options

$scope.optionString更改为

$scope.optionString = "value as value for (key,value) in elements";

请参阅documentation了解对象数据源。

<强> FULL EXAMPLE