diff --git a/lib/ng2-select2.component.ts b/lib/ng2-select2.component.ts index 1607f94..4e22a81 100644 --- a/lib/ng2-select2.component.ts +++ b/lib/ng2-select2.component.ts @@ -98,7 +98,22 @@ export class Select2Component implements AfterViewInit, OnChanges, OnDestroy, On this.setElementValue(this.value); } - this.element.on('select2:select select2:unselect', () => { + this.element.on('select2:select', () => { + this.valueChanged.emit({ + value: this.element.val(), + data: this.element.select2('data') + }); + }); + + this.element.on('select2:unselect', () => { + /* for some reason the element is still returned by val. Workaround for single-select controls */ + if (this.options.multiple !== true) { + this.valueChanged.emit({ + value: null, + data: this.element.select2('data') + }); + return; + } this.valueChanged.emit({ value: this.element.val(), data: this.element.select2('data') @@ -107,7 +122,9 @@ export class Select2Component implements AfterViewInit, OnChanges, OnDestroy, On } ngOnDestroy() { - this.element.off("select2:select"); + if (this.element) { + this.element.off("select2:select"); + } } private initPlugin() {