Created
July 7, 2016 16:42
-
-
Save LA1CH3/718588765d56a8932de52c64c3561dcf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
SEO Service for Updating Title, Meta Tags, Etc. | |
*/ | |
import { Injectable } from '@angular/core'; | |
import { Title } from '@angular/platform-browser'; | |
import { getDOM } from '@angular/platform-browser/src/dom/dom_adapter'; | |
@Injectable() | |
export class SeoService { | |
private titleService: Title; | |
private headElement: HTMLElement; | |
private metaDescription: HTMLElement; | |
private robots: HTMLElement; | |
private DOM: any; | |
constructor(titleService: Title){ | |
this.titleService = titleService; | |
this.DOM = getDOM(); | |
this.headElement = this.DOM.query('head'); | |
this.metaDescription = this.getOrCreateMetaElement('description'); | |
this.robots = this.getOrCreateMetaElement('robots'); | |
} | |
public getTitle(): string { | |
return this.titleService.getTitle(); | |
} | |
public setTitle(newTitle: string) { | |
this.titleService.setTitle(newTitle); | |
} | |
public getMetaDescription(): string { | |
return this.metaDescription.getAttribute('content'); | |
} | |
public setMetaDescription(description: string) { | |
this.metaDescription.setAttribute('content', description); | |
} | |
public getMetaRobots(): string { | |
return this.robots.getAttribute('content'); | |
} | |
public setMetaRobots(robots: string) { | |
this.robots.setAttribute('content', robots); | |
} | |
/** | |
* get the HTML Element when it is in the markup, or create it. | |
* @param name | |
* @returns {HTMLElement} | |
*/ | |
private getOrCreateMetaElement(name: string): HTMLElement { | |
let el: HTMLElement; | |
el = this.DOM.query('meta[name=' + name + ']'); | |
if (el === null) { | |
el = this.DOM.createElement('meta'); | |
el.setAttribute('name', name); | |
this.headElement.appendChild(el); | |
} | |
return el; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to create element meta property and og:title?