开发者问题收集

Angularjs 多选:无法读取未定义的属性“长度”

2018-01-02
6251

我尝试过很多变化,但似乎都没有用。

我收到以下错误:

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});
}); 

我可以看到我的选择中的选项,但是每次我单击任何选项时,都会出现该错误。以下是显示来自 success.data.d.results 的信息的下拉列表:

dropdown

3个回答

这是我的代码现在的样子,它正在运行。感谢@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;
 }
lumayara
2018-01-19

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

Priyanka Arora
2019-11-08

您应该初始化您的选择数组,

$scope.addRecipient = {}; 
$scope.addRecipient.selection = [];
NTP
2018-01-02