If you're open to integrating a fresh library, consider utilizing the HeadService
from @bespunky/angular-zen
. This service offers a secure and typed access to the head element.
Your component or service could resemble the following setup:
import { Component, OnInit } from '@angular/core';
import { HeadService } from '@bespunky/angular-zen/core';
@Component({
selector : 'zen-head-service-demo',
templateUrl: './head-service-demo.component.html',
styleUrls : ['./head-service-demo.component.css']
})
export class HeadServiceDemoComponent implements OnInit
{
constructor(private head: HeadService) { }
ngOnInit()
{
this.head.addElement('script', /* config object or config function */);
}
}
Check out live examples here | Access the service documentation | Explore the Service API
🙋♂️ Don't forget to run
npm install @bespunky/angular-zen
and import CoreModule
from the /core
package.
TLDR
💡 The HeadService
offers additional features like searching for elements by attributes, support for common elements (e.g. scripts and styles), etc.
💡 It's also designed for easy testing, allowing for document element mocking and spying functionalities.