diff --git a/src/ol/interaction/Select.js b/src/ol/interaction/Select.js index 11a3c74bd0..3bd762db51 100644 --- a/src/ol/interaction/Select.js +++ b/src/ol/interaction/Select.js @@ -167,6 +167,16 @@ class Select extends Interaction { const options = opt_options ? opt_options : {}; + /** + * @private + */ + this.boundAddFeature_ = this.addFeature_.bind(this); + + /** + * @private + */ + this.boundRemoveFeature_ = this.removeFeature_.bind(this); + /** * @private * @type {import("../events/condition.js").Condition} @@ -250,9 +260,8 @@ class Select extends Interaction { */ this.featureLayerAssociation_ = {}; - const features = this.getFeatures(); - features.addEventListener(CollectionEventType.ADD, this.addFeature_.bind(this)); - features.addEventListener(CollectionEventType.REMOVE, this.removeFeature_.bind(this)); + this.features_.addEventListener(CollectionEventType.ADD, this.boundAddFeature_); + this.features_.addEventListener(CollectionEventType.REMOVE, this.boundRemoveFeature_); } /** @@ -323,6 +332,10 @@ class Select extends Interaction { if (map && this.style_) { this.features_.forEach(this.applySelectedStyle_.bind(this)); } + if (!map) { + this.features_.removeEventListener(CollectionEventType.ADD, this.boundAddFeature_); + this.features_.removeEventListener(CollectionEventType.REMOVE, this.boundRemoveFeature_); + } } /**