ngOnDestroy() { // destroy these components else there will be memory leaks this.embeddedComponents.forEach(comp => comp.destroy()); this.embeddedComponents.length = 0; }
// security: the doc.content is always authored by the documentation team // and is considered to be safe this.hostElement.innerHTML = doc.contents || '';
if (!doc.contents) { return; }
this.addTitleAndToc(doc.id);
// TODO(i): why can't I use for-of? why doesn't typescript like Map#value() iterators? this.embeddedComponentFactories.forEach(({ contentPropertyName, factory }, selector) => { const embeddedComponentElements = this.hostElement.querySelectorAll(selector);
// cast due to https://github.com/Microsoft/TypeScript/issues/4947 for (const element of embeddedComponentElements asanyasHTMLElement[]){ // hack: preserve the current element content because the factory will empty it out // security: the source of this innerHTML is always authored by the documentation team // and is considered to be safe element[contentPropertyName] = element.innerHTML; this.embeddedComponents.push(factory.create(this.injector, [], element)); } }); }
privateaddTitleAndToc(docId: string) { this.tocService.reset(); let title = ''; const titleEl = this.hostElement.querySelector('h1'); // Only create TOC for docs with an <h1> title // If you don't want a TOC, add "no-toc" class to <h1> if (titleEl) { title = titleEl.innerText.trim(); if (!/(no-toc|notoc)/i.test(titleEl.className)) { this.tocService.genToc(this.hostElement, docId); titleEl.insertAdjacentHTML('afterend', '<aio-toc class="embedded"></aio-toc>'); } } this.titleService.setTitle(title ? `Angular - ${title}` : 'Angular'); }