Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Typescript

How to Set HTML Meta Tags in Angular

5.00/5 (1 vote)
12 Jan 2022CPOL3 min read 19K  
Utilize Meta service to add, remove and update meta tags in Angular app
In this article, we'll understand how we could utilize the Meta service to add, remove, and update meta tags in your Angular application.

Introduction

Meta tags are simply a small amount of HTML code that is used to describe the content of your web page. Meta tags provide metadata about the HTML document and are typically used to provide information about webpage content, author, keywords, viewport settings, etc. Meta tags usually go inside the <head> section of your HTML document and are not visible on the webpage. These tags are primarily used by web browsers, search engines, and other web services to understand the content of the webpage, e.g., The viewport tag is used by web browsers to gather information about the webpage's dimensions and scaling.

Angular comes bundled with Meta service which we could use to work with the meta tags. In this article, we'll understand how we could utilize this service to add, remove, and update meta tags in your Angular application.

Importing the Meta Service

Before using the meta service, we need to import it from the platform browser package.

TypeScript
import { Meta } from '@angular/platform-browser';

Next, we'll inject it inside our component using via the constructor.

TypeScript
constructor(private meta: Meta) {}

Adding Meta Tags

To add new meta tags, the Angular Meta service provides two methods, addTag and addTags.

TypeScript
this.meta.addTag({ name: 'description', 
                   content: 'This is an article about Angular Meta service' });

The addTag method accepts a meta definition object as a parameter that is used to describe the meta tag.

The above code will result in following HTML meta tag element.

TypeScript
<meta name="description" content="This is an article about Angular Meta service">

You could also use addTags to add multiple meta tags at the same time.

TypeScript
this.meta.addTags([ 
    { name: 'description', content: 'This is an article about Angular Meta service' }, 
    { name: 'keywords', content: 'angular, javascript, typescript, meta, seo' } 
]);

The above code will result in the following HTML meta tag elements:

TypeScript
<meta name="description" content="This is an article about Angular Meta service">
<meta name="keywords" content="angular, javascript, typescript, meta, seo">

One thing to keep in mind is both addTag and addTags methods also accept a second parameter, forceCreation, which forces the methods to create a new meta tag element without checking if it already exists.

Retrieving Meta Tags

To read the meta tags from the DOM, we could use the getTag or getTags methods provided by the Meta service. The getTag and getTags method accept a string that represents an attribute selector and returns the matching element based on that string:

TypeScript
const keywords = this.meta.getTag('name=keywords'); 
console.log(keywords.content); 
// Output: angular, javascript, typescript, meta, seo

The getTag method returns an HTMLMetaElement while getTags returns array of HTMLMetaElements.

One thing to keep in mind is getTag returns the first instance of matching meta tag described in the selector argument whereas the getTags method returns all the instances of meta tags that match the selector.

TypeScript
let tags = this.meta.getTags('name');

In the above code, getTags will return all the instances of meta tags that contains the name attribute and will save those instances in the form of an array inside the tags variable.

Updating Meta Tags

To update existing meta tags, we could use the updateTag method that comes bundled with the Angular Meta service.

TypeScript
this.meta.addTag({ name: 'keywords', content: 'angular, javascript, typescript, meta, seo' }); 
setTimeout(() => { 
    this.meta.updateTag( 
        { name: 'keywords', content: 'angular, javascript, typescript, meta' }, 
        'name=keywords' 
)}, 4000)

In the above code snippet, we're first adding a new meta tag with name keywords. Next, we're updating the tag after 4 seconds using the updateTag method. One thing to keep in mind is if the tag with name keywords doesn't exist in the DOM, the updateTag method will create a new element.

Removing Meta Tags

The removeTag method accepts a selector string as an argument and removes the tag if the element matching the selector is found.

TypeScript
this.meta.removeTag('name=keywords');

The above code searches for meta tag with name as keywords, and if the match is found, it'll simply remove that meta tag from the DOM.

The Meta service also provides a removeTagElement method that could be used to remove a meta tag. However, unlike the removeTag method, it accepts HTMLMetaElement and removes that element from the DOM.

TypeScript
const keywords = this.meta.getTag('name=keywords');
this.meta.removeTagElement(keywords);

The above code first selects the keywords meta tag element from the DOM and then passes it as an argument to the removeTagElement method which removes the element from the DOM.

And that's it!

For more information, check out:

I hope you found this article useful. In case you've any queries or feedback regarding the same, feel free to drop a comment in the comment section.

History

  • 12th January, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)