import angular, {IDirective} from 'angular'; /** * @type {angular.IModule} */ const myModule = angular.module('ngeoEvent', []); /** * This directive makes a bridge between custom events and AngularJS. * It allows to listen to custom events and react to changes with AngularJS. * Example: * * @returns {angular.IDirective} Directive Definition Object. */ const ngeoEventDirective = () => { return { restrict: 'A', scope: { 'model': '=ngeoEventModel', 'on': ' void; if (!eventName) { console.error('Missing event name, did you forget the quotes around the "on" attribute?'); return; } if (!cb) { console.error('Missing callback, did you forget the callback "cb" attribute?'); return; } // On event listened, update the model with the value and // let AngularJs know the value by running an apply. // then run a second apply to call the callback with the updated model. element[0].addEventListener(eventName, (event: Event) => { const cEvent: CustomEvent = event as CustomEvent; scope.$apply(() => { // @ts-ignore: scope.cb // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment scope.model = cEvent.detail; }); scope.$apply(() => { cb(); }); }); }, } as IDirective; }; myModule.directive('ngeoEvent', ngeoEventDirective); export default myModule;