Я реализовал (ngModelChange) в моем модуле ng-multiselect-dropdown, чтобы при любом выборе или отмене выбора любого значения я соответственно помещал его в массив.Но как только я загружаю приложение, функция onChange (), которая включает ngModelChange, загружается каждый раз и выдает ошибку -
Невозможно прочитать свойство 'length' с неопределенным значением
Мой код -
app.component.ts
onChange(item: any) {
this.selectedItems=[];
console.log("inside onChange ,item = ", item)
console.log("inside onChange, selectedItemsSelected =", this.selectedItemsSelected);
console.log("inside onChange, this.selectedItems = ", this.selectedItems)
if (this.selectedItemsSelected.length > 0) {
for (var i = 0; i < this.selectedItemsSelected.length; i++) {
this.selectedItems.push(this.selectedItemsSelected[i].item_text);
console.log("inside onChange, this.selectedItems = ", this.selectedItems)
}
console.log("End of onChange, this.selectedItems = ", this.selectedItems)
}
}
app.component.html
<p style="width:50%">
<ng-multiselect-dropdown [placeholder]="'Select API Object'" [data]="dropdownListSelected" [(ngModel)]="selectedItemsSelected"
[settings]="dropdownSettingsSelected" (ngModelChange)="onChange($event)" (onSelect)="onItemSelectSelected($event)" (onSelectAll)="onSelectAllSelected($event)">
</ng-multiselect-dropdown>
</p>
вывод - при компиляции
inside onChange ,item = undefined
undefined
inside onChange, selectedItemsSelected = undefined
inside onChange, this.selectedItems = Array(0)
ERROR TypeError: Cannot read property 'length' of undefined
at NodeVennComponent.push../src/app/api-mining/api-venn/node-venn/node-venn.component.ts.NodeVennComponent.onChange (node-venn.component.ts:409)
at Object.eval [as handleEvent]
at handleEvent (core.js:10251)
at callWithDebugContext (core.js:11344)
at Object.debugHandleEvent [as handleEvent] (core.js:11047)
at dispatchEvent (core.js:7710)
at core.js:9190
at SafeSubscriber.schedulerFn [as _next] (core.js:3563)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:196)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:134)