Solution: Observe bootstrap events with Angular4 / Bootstrap4.
Summary: Capturing bootstrap events and disabling a special event in its place. Custom event will be observed from angular components.
Index.html:
<body> ... <script> function eventRelay(bs_event){ $('body').on(bs_event, function($event){ const customEvent = document.createEvent('Event'); customEvent.initEvent(bs_event, true, true); let target_id = '#'+$event.target.id; $(target_id)[0].dispatchEvent(customEvent); }); </script> </body>
In component / service:
//dynamically execute the event relays private registerEvent(event){ var script = document.createElement('script'); script.innerHTML = "eventRelay('"+event+"');" document.body.appendChild(script); }
In your component constructor or ngOnInit (), you can now log and monitor boot events. For example, the "hidden" event Bootstrap Modal.
constructor(){ registerEvent('hidden.bs.modal'); Observable.fromEvent(document, 'hidden.bs.modal') .subscribe(($event) => { console.log('my component observed hidden.bs.modal');
Note: the eventRelay function must be inside index.html for the DOM to load it. Otherwise, it will not be recognized if you call the eventRelay calls from registerEvent.
Conclusion: this is a middleware solution that works with vanilla Angular4 / Bootstrap4. I do not know why bootstrap events are not displayed inside angular, and I did not find another solution.
Note1: Register the register only once for each event. This means βonceβ throughout the application, so consider entering all registerEvent calls into app.component.ts. Calling registerEvent several times will cause recurring events to be emitted before angular.
Note2: There is an alternative bootstrap structure that you can use with angular called ngx-bootstrap ( https://valor-software.com/ngx-bootstrap/#/ ), which can do but I have not tested this.
Advanced solution. Create an angular service that manages events registered through "registerEvent", so for each event it only calls "registerEvent". Thus, in your components, you can call "registerEvent" and then immediately create an Observable for this event, without worrying about duplicate calls to "registerEvent" in other components.