Skip to content

Instantly share code, notes, and snippets.

@kuldeepkeshwar
Created October 28, 2016 09:05
Show Gist options
  • Save kuldeepkeshwar/889d26b264b3e1c7fda07704e0a65211 to your computer and use it in GitHub Desktop.
Save kuldeepkeshwar/889d26b264b3e1c7fda07704e0a65211 to your computer and use it in GitHub Desktop.
seo service for angular2
export class SeoMetaData{
title: string;
description:string;
}
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