import { Inject , Injectable , RendererFactory2 , ViewEncapsulation } from '@angular/core' ; import { DOCUMENT } from '@angular/platform-browser' ; import { MetaService } from '@ngx-meta/core' ; @ Injectable ( ) export class SeoService { private baseTitle : string ; private headElement : HTMLElement ; private robots : HTMLElement ; private DOM : any ; private rendererFactory : RendererFactory2 ; constructor ( @ Inject ( DOCUMENT ) document , @ Inject ( RendererFactory2 ) rendererFactory , private readonly meta ) { this . baseTitle = '· Code.gov' ; this . rendererFactory = rendererFactory ; this . DOM = document ; this . headElement = this . DOM . head ; this . robots = this . getOrCreateMetaElement ( 'robots' ) ; } public setTitle ( newTitle , baseTitle = false ) { if ( baseTitle === true ) newTitle += ' · Code.gov' ; this . meta . setTitle ( newTitle ) ; } public setMetaDescription ( description ) { this . meta . setTag ( 'description' , description ) ; } public getMetaRobots ( ) { return this . robots . getAttribute ( 'content' ) ; } public setMetaRobots ( robots ) { this . robots . setAttribute ( 'content' , robots ) ; } private getOrCreateMetaElement ( name ) { let el ; el = this . DOM . querySelector ( 'meta[name=' + name + ']' ) ; if ( el === null ) { const renderer = this . rendererFactory . createRenderer ( this . DOM , { id : '-1' , encapsulation : ViewEncapsulation . None , styles : [ ] , data : { } } ) ; el = renderer . createElement ( 'meta' ) ; renderer . setAttribute ( el , 'name' , name ) ; renderer . appendChild ( this . headElement , el ) ; } return el ; } }