I want to know what event will be fired when the innerHtml
property of any div
is changed and DOM the tree for that div
is completely loaded into memory.
I want to invoke the following function after that event is fired. I want to remove the setTimeout()
hack in the following function as it may fail sometime.
private registerEventListenersForLink() {
let _self = this;
setTimeout(function () {
var AElemList = document.querySelectorAll('.appmedia-content-wrapper a');
for (let i = 0; i < AElemList.length; i++) {
AElemList[i].addEventListener("click", function (event) {
event.preventDefault();
event.stopPropagation();
var url = event.target["href"];
if (url && url.trim() != '') {
_self.utilService.openUrlExternaly(url);
}
});
}
}, 80);
}
I have found following links related to above issue but I haven't got any ideas out of it:
Event to determine when innerHTML has loaded
https://www.w3.org/html/wg/spec/apis-in-html-documents.html#dom-innerhtml
I fixed my issue. I have implemented a directive as follows and it has worked like a charm. Thank you Rory McCrossan for pointing me at MutationObserver api.
import { Directive, ElementRef } from '@angular/core';
import { UtilService } from '../../providers/util-service';
@Directive({
selector: '[external-links]' // Attribute selector
})
export class ExternalLinks {
private observer;
constructor(private elRef: ElementRef, public utilService: UtilService) {
}
ngAfterViewInit() {
var _self = this;
this.observer = new MutationObserver(mutations => {
mutations.forEach(function (mutation) {
if (mutation.type == 'childList') {
var AElemList = _self.elRef.nativeElement.querySelectorAll('a');
for (let i = 0; i < AElemList.length; i++) {
AElemList[i].addEventListener("click", function (event) {
event.preventDefault();
event.stopPropagation();
var url = event.target["href"];
if (url && url.trim() != '') {
_self.utilService.openUrlExternaly(url);
}
});
}
}
});
});
var config = { childList: true };
this.observer.observe(this.elRef.nativeElement, config);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.