Angularjs将从下拉列表中选择的项添加到作用域

angularjs adding selected items from dropdowns to the scope

本文关键字:添加 作用域 选择 下拉列表 Angularjs      更新时间:2023-09-26

我有一个表,每个列上面有一个下拉列表,其中列的数量是动态的。我创建了如下命令

            <table class='table' >
                <tr>
                    <th ng-repeat= "item in importTable[0]">
                        <select ng-model="selectedItem" ng-options="i.Name for i in optionList"></select>
                    </th>
                </tr>
                <tr ng-repeat="row in importTable">
                    <td ng-repeat="item in row">{{ item }} </td>
                </tr>
            </table>

其中optionList是下拉列表中的选项列表。所有的下拉列表都有相同的optionList。

我如何将选中的项目与列的索引一起添加到模型的作用域?

这里是一个链接到JSfiddle http://jsfiddle.net/U3pVM/769/只需点击导入。我希望能够定义哪个列是哪个类型

你可以使用ngRepeat提供的$index变量将ngModel设置为数组中的特定项:

首先在你的控制器中定义一个数组,它将包含所有的模型:

function ImportCtrl($scope) {   
  $scope.selectedItems = [];
  ...
}

然后,在ngRepeat中,你把ngModel引用到selectedItems数组中的一个特定项:

<select ng-model="selectedItems[$index]" ng-options="i.Name for i in columnNames"></select>
演示小提琴