Angularjs 多个 Select: 无法读取未定义的 属性 'length'

Angularjs Multiple Select: Cannot read property 'length' of undefined

我尝试了很多变体,但似乎没有任何效果。

我收到以下错误:

TypeError: Cannot read property 'length' of undefined
at select.js:1560
at m.$broadcast (angular.js:18487)
at Object.ctrl.select (select.js:673)
at fn (eval at compile (angular.js:15358), <anonymous>:4:453)
at e (angular.js:26994)
at b.$eval (angular.js:18161)
at b.$apply (angular.js:18261)
at HTMLDivElement.<anonymous> (angular.js:26999)
at HTMLDivElement.dispatch (jquery-3.2.1.min.js:3)
at HTMLDivElement.q.handle (jquery-3.2.1.min.js:3)

我在HTML部分的代码是:

<label for="addRecipient" class="bold">Additional Recipients:</label>
<ui-select multiple ng-model="addRecipient.selection" theme="bootstrap" title="Choose Recipients" class="multipleSelect">
<ui-select-match placeholder="Select Additional Recipients...">{{$item.name}}</ui-select-match>
<ui-select-choices repeat="additional in addRecipients | filter:$select.search">
 {{additional.name}}
</ui-select-choices>
</ui-select>

在我的控制器中:

$scope.addRecipient = {}; 
$scope.addRecipients =  [];
$.each(success.data.d.results, function (ind, val) {
  $scope.addRecipients.push({'name': val.Title, 'email':val.Email});
}); 

我可以在我的 select 中看到 select 的选项,但是每次我点击任何选项时,我都会收到该错误。这是显示来自 success.data.d.results:

的信息的下拉菜单

您应该初始化选择数组,

$scope.addRecipient = {}; 
$scope.addRecipient.selection = [];

这就是我的代码现在的样子,它正在运行。谢谢@NTP 的提示!

<label for="addRecipient" class="bold">Additional Recipients:</label>
<ui-select multiple ng-model="addRecipient.selected" theme="bootstrap" title="Choose Recipients" class="multipleSelect">
   <ui-select-match placeholder="Select Additional Recipients...">{{$item.Title}}</ui-select-match>
   <ui-select-choices repeat="additional in addRecipients | filter:$select.search">
       {{additional.Title}}
</ui-select-choices>

在控制器中:

$scope.addRecipient = {}; 
$scope.addRecipients = [];

下拉值:

$scope.addRecipients = success.data.d.results;

正在获取选定的值:

//add Recipient
var addRecipients;
if ($scope.addRecipient.selected && $scope.addRecipient.selected !='undefined'){
     things = [];
     console.log($scope.addRecipient.selected);
     for (var key in $scope.addRecipient.selected) {
          if (key < $scope.addRecipient.selected.length) {
                 things.push($scope.addRecipient.selected[key].Email) ;
          }
      }
      addRecipients = things;
 }

当 dropdownList 中的数据为空时,我收到了错误。在 dropdownList 中初始化值后,它就可以正常工作。 this.dropdownList=[];