如何从 ui-selects in ng-repeat 中获取选定值

How to get selected value from ui-selects in ng-repeat

我正在用 ng-repeat 添加 ui-selects 但我不知道如何分别从每个 ui-select 中获取值控制器。我想在控制器中获取 selected 项目值,用作发送 $http 请求的参数。

<div ng-repeat="repeat in repeats">
   <p>Selected: {{pattern.selected.name}}</p>
   <ui-select ng-model="repeat.id">
<ui-select-match placeholder="Enter an pattern...">{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="pattern in data_pattern_newspapers track by $index">
  <div ng-bind-html="pattern.name | highlight: $select.search"></div>
</ui-select-choices>

我想在我的控制器中执行以下操作:

alert($scope.repeat.id.selected)

如何访问控制器中每个 ui-select 的模型?

我的 plnkr

请一些人帮助我。谢谢!

在你的 html 中利用 ngRepeat 的 $index 属性 为每个 ui-select 创建一个独特的模型,并将 $index 传递给 ngChange 函数:

<ui-select ng-model="selections[$index]" ng-change="selectionChanged($index)">

在你的控制器中初始化一个数组来保存选定的值,并添加上面的接收 $index 的函数来引用这个数组:

$scope.selections = [];

$scope.selectionChanged = function(idx) {
  console.log(idx, $scope.selections[idx]);
};

现在 $scope.selections 是他们选择的数组。

演示:http://plnkr.co/edit/NSK3UmelATopV0Juw8BV?p=preview