I've seen a number of posts addressing similar issues, but despite my best effort to follow the recommended approach, adding tags using the Meta service still doesn't work for me. So I decided to create a simple Angular app adding the Meta service with no other functionality and try to add a meta-tag.
Here's my AppComponent:
import { Component } from '@angular/core';
import { Meta } from '@angular/platform-browser';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private metaService: Meta) {
this.metaService.addTag({ property: 'og:title', content: 'constructor'});
}
ngOnInit() {
this.metaService.addTag({ property: 'og:title', content: 'ngOnInit'});
}
title = 'ng-app';
}
What I expect here is that after opening the main page and viewing the source, I will see the following meta-tag:
<meta property="og:title" content="constructor">
or
<meta property="og:title" content="constructor">
But, neither appears when I view the source. Here's the output:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>NgApp</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.css"></head>
<body>
<app-root></app-root>
<script src="runtime.js" type="module"></script><script src="polyfills.js" type="module"></script><script src="styles.js" defer></script><script src="vendor.js" type="module"></script><script src="main.js" type="module"></script></body>
</html>
I'm really puzzled. Either I don't understand how this should work and it won't work the way I would like by design, or I'm missing something.
For full transparency, I've uploaded this very basic project to my github: https://github.com/0x4ndy/ng-app
If you can have a look and try this out, I will be grateful. Any hints are very much appreciated!
EDIT: as it is explained in the answer to this question, it won't be possible to update the static meta-tags and the backend rendering of the page is required to achieve this.