Created
October 28, 2016 09:05
-
-
Save kuldeepkeshwar/889d26b264b3e1c7fda07704e0a65211 to your computer and use it in GitHub Desktop.
seo service for angular2
This file contains hidden or 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
export class SeoMetaData{ | |
title: string; | |
description:string; | |
} |
This file contains hidden or 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
import { Injectable, Inject } from '@angular/core'; | |
import { DOCUMENT } from '@angular/platform-browser'; | |
import { SeoMetaData } from "seo-meta-data"; | |
@Injectable() | |
export class SEOService { | |
constructor( @Inject(DOCUMENT) private document: any) {} | |
public setData(metaData: SeoMetaData): void { | |
this.setMeta(metaData.title,metaData.description); | |
} | |
private setMeta(title: string = '', description: string = '') { | |
this.setTitle(title); | |
this.setMetaDescription(description); | |
} | |
private setTitle(title: string) { | |
this.document.title = title; | |
} | |
private setMetaDescription(description: string) { | |
let headChildren = this.document.head.children; | |
for (let i = 0; i < headChildren.length; i++) { | |
let element = headChildren[i]; | |
if(element.name === 'meta' && element.attribs.name === 'description'){ | |
element.attribs.content = description; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment