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.
- import { Meta } from '@angular/platform-browser';
Next, we'll inject it inside our component using via the constructor.
- constructor(private meta: Meta) {}
Adding Meta Tags
To add new meta tags, the Angular Meta service provides two methods addTag and addTags.
- this.meta.addTag({ name: 'description', content: 'This is an article about Angular Meta service' });
The above code will result in following HTML meta tag element.
- <meta name="description" content="This is an article about Angular Meta service">
- this.meta.addTags([
- { name: 'description', content: 'This is an article about Angular Meta service' },
- { name: 'keywords', content: 'angular, javascript, typescript, meta, seo' }
- ]);
- <meta name="description" content="This is an article about Angular Meta service">
- <meta name="keywords" content="angular, javascript, typescript, meta, seo">
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:
- 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.
- let tags = this.meta.getTags('name');
Updating Meta tags
To update existing meta tags we could use the updateTag method that comes bundled with the Angular Meta service.
- 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)
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.
- this.meta.removeTag('name=keywords');
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.
- const keywords = this.meta.getTag('name=keywords')
- this.meta.removeTagElement(keywords);
And that's it!
For more information,
- Meta Tags: https://developer.mozilla.org/en-US/docs/Web/HTML/Viewport_meta_tag
- Meta service: https://angular.io/api/platform-browser/Meta
- Meta Definition: https://angular.io/api/platform-browser/MetaDefinition
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.

ansh shriPosted Sep 13, 2021, 12:33 PM
Hi Harshal, I am updating meta tags & it is only showing in inspect head section, not in page view source. Does google reads from head section? Thanks