I am trying to trigger the (ionic) click event of an element manually, from jquery or native dom events.
Let's say for example I have the following:
entry.html
<button [id]="id" (click)="entryClicked()></button>
entry.ts
@Component({
selector: 'entry',
templateUrl: 'entry.html',
})
export class EntryComponent {
constructor() {}
id = "entry_1"
entryClicked () {
console.log(id +" has been clicked")
}
}
From another service, I would like to trigger the click event on entry_1.
Problem: this other service is completely separate from the first, it's a provider and to my knowledge, I cannot use @ViewChild and @ViewChildren from it. So I cannot directly access functions of the entry component.
externalService.ts
import { Injectable } from '@angular/core';
import * as $ from 'jquery';
@Injectable()
export class ExternalServiceProvider {
constructor( ) { }
someEvent () {
$("#entry_1").click();
}
}
Triggering someEvent() doesn't trigger entryClicked() like I hope it would.
It's not a problem of loading, the #entry_1 element is found by jquery. It's just that the (click) event bound to the element by ionic doesn't seem to be related to the jquery click event.
Same problem if I use $("#entry_1")[0].click().
However, if I click the element in the page, it fires properly.
How can I fire the original ionic click event from the DOM, with or without jquery?
EntryComponentfrom theExternalServiceProvider. I don't see any way to do that otherwise (and would love to have another solution). The entry is created by an automatic synchronization in the database, and therefore I don't have access to the Component from the entry creation callback. Not sure I'm very clear, but believe me, I tried finding better ways to do that, and redesigning a, by many aspect well designed app, just for this improvement is not worth it. The click workaround would be a very good option at the moment.