I\'m attempting to inject a string that contains a (click) event into the Angular2 template. The string is dynamically retrieved from the back-end much after t
I had problems importing ElementRef in my service and did not want to pass Renderer reference there. This Service is about showing 'Loading...' dialog where after 3 seconds dynamic close button is attached. I found solution how to add click event for this button using jQuery. Hopefully this helps someone.
(1) Add jQuery to Angular 2 index.html file
(2) Declare $
declare var $: any;
(3) Use jQuery This is piece of code from my service:
public async present() {
this.isLoading = true;
return await this.loadingController.create({message: 'Loading...'}).then(a => {
a.present().then(() => {
setTimeout(() => {
a.message += ' ';
// Here starts jQuery usage
$(a).click((clickedObject) => {
if ($(clickedObject.target).hasClass('alert-cancel-button')) {
this.dismiss();
}
});
}, 3000);
});
});
}
public async dismiss() {
return await this.loadingController.dismiss();
}
$('alert-cancel-button').click(() => this.dismiss()) didn't work.