Learn how to add/update HTML Meta tags using the Meta Service in Angular. The Angular Meta Service makes it easier to set different meta tags to different pages. It provides methods such as addTag(), addTags(), getTag(), getTags(), updateTag(), removeTag(), removeTagElement() etc. We can use them to manipulate meta tags. Let us find out how to use Meta service using an example.
The Meta service in Angular provides the following methods to manipulate the HTML Meta tags.
To use meta service, we first need to import it in the Root module. The Meta service is part of the @angular/platform-browser library as it is applicable only apps running on the browser.
import { BrowserModule, Meta } from '@angular/platform-browser';
Next, we need to register it in the Angular Providers metadata as shown below
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [Meta],
bootstrap: [AppComponent]
})
export class AppModule { }
To make use of the service in components, all you need to do is to inject it using Dependency Injection.
Then call any of the methods of the Meta Service to manipulate the Tags. You can either use ngOnInit or constructor.
import { Component, OnInit } from '@angular/core';
import { Meta, MetaDefinition } from '@angular/platform-browser';
@Component({
template: `<h1>Home Component</h1>`
})
export class HomeComponent implements OnInit {
title = 'Home Component Title';
constructor(private metaService:Meta){
}
ngOnInit() {
this.metaService.addTag( { name:'description',content:"Article Description"});
}
}
Meta definition class represents a meta element.
type MetaDefinition = {
charset?: string;
content?: string;
httpEquiv?: string;
id?: string;
itemprop?: string;
name?: string;
property?: string;
scheme?: string;
url?: string;
} & {
[prop: string]: string;
};
We create objects of type MetaDefinition and add them using the Meta Service. For Example
{name: 'description', content: 'Title and Meta tags examples'}
and it results in the following HTML
<meta name=”description” content=”Setting HTML Meta Tags Angular Meta service Example” />
Similarly,
{charset: 'UTF-8'}
=> <meta charset="UTF-8">
{meta name="robots" content="Index,follow"}
====> <meta name=”robots” content=”Index,follow” />
{meta name="viewport" content="width=device-width, initial-scale=1"}
====> <meta name="viewport" content="width=device-width, initial-scale=1">
updateTag() method updates the existing meta tags with the new one (first argument) based on search criteria. Specify the search criteria as the second argument in the form attributeName='value' similar to getTag method
Syntax
updateTag(tag: MetaDefinition, selector?: string): HTMLMetaElement | null
//Add the Tag
this.metaService.addTag({ name: 'robots', content: 'index,follow' });
//output
<meta name="robots" content="index, follow">
//Now update the Tag
this.metaService.updateTag( { name:'robots', content:'index, nofoloow'},"name='robots'");
//Output
<meta name="robots" content="index, nofoloow">
updateTag inserts the tag if matching meta element is not found
this.metaService.updateTag( { name:'description', content:'Article Description'},"name='description'");
//Will insert the Meta if it is not found
<meta name="description" content="Article Description">
updateTag replaces only the first instance of the search criteria.
this.metaService.addTag({ property: 'og:title', content: 'Social Media descripton'});
this.metaService.addTag({ property: 'og:title', content: 'Duplicate Social Media descripton'});
//output
<meta property="og:title" content="Social Media descripton">
<meta property="og:title" content="Duplicate Social Media descripton">
//Now run this
this.metaService.updateTag( { property:'og:title', content:Social Media 'Description of the component'},"property='og:title'");
//output
<meta property="og:title" content="Description of the componen"> //Updated
<meta property="og:title" content="Duplicate Social Media descripton"> //Not touched
Use removeTag() or removeTagElement() element to remove the meta tag.
Syntax
removeTag(attrSelector: string): void
removeTagElement(meta: HTMLMetaElement): void
The following example adds the robots metatag and then uses the removeTag to remove it. removeTag removes the first instance of the matching meta tag. You need to pass the name of the attribute and value in the format attributeName='value'. You can search using only one attribute.
Example
//Adding the Meta Tag
this.metaService.addTag({ name: 'robots', content: 'index,follow' });
//Output
<meta name="robots" content="index,follow">
//Remove it
this.metaService.removeTag("name='robots'");
Removes the first matching tag.
//Adds tow meta tags
this.metaService.addTag({ property: 'og:title', content: 'Social Media descripton'});
this.metaService.addTag({ property: 'og:title', content: 'Duplicate Social Media descripton'});
//This will remove the first one
this.metaService.removeTag("property='og:title'");
You can use any attribute of the meta. The following example uses the content attribute to remove it.
//Adds the meta tag
this.metaService.addTag({ property: 'og:title', content: 'Duplicate Social Media descripton'});
//Output
<meta property="og:title" content="Duplicate Social Media descripton">
//This will remove it
this.metaService.removeTag("content='Duplicate Social Media descripton'");
You can also use removeTagElement() to remove the meta. You need to pass the HTMLMetaElement , which you want to remove. For example,
//Ads two meta property
this.metaService.addTag({ property: 'og:title', content: 'Social Media descripton'});
this.metaService.addTag({ property: 'og:title', content: 'Duplicate Social Media descripton'});
//Removes the first instance of the 'og:title'
this.metaService.removeTagElement(this.metaService.getTag('property'));