Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
In angular 2 how can I make the canonical tag dynamic per page

In angular 2 how can I make the canonical tag dynamic per page.

This is my index page tag for it:

  <link rel="canonical" href="https://mywebsite.co.uk" />

How can I make it dynamic e.g. if on blog page it should look like this at run time:

  <link rel="canonical" href="https://mywebsite.co.uk/blog" />

I'm using angular version 4, webpack and typescript with ng2-metadata to change the title, description and keywords of all my urls.

I just need the canonical tag to change for the seo google bot.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I implemented a service that is called in the ngOnInit functions of my page components:

import { Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/common';

@Injectable()
export class LinkService {

  private link: HTMLLinkElement;

  constructor(@Inject(DOCUMENT) private doc) { }

  createLinkForCanonicalURL() {
    if (this.link === undefined) {
      this.link = this.doc.createElement('link');
      this.link.setAttribute('rel', 'canonical');
      this.doc.head.appendChild(this.link);
    }
    this.link.setAttribute('href', this.doc.URL.split('?')[0]);
  }
}

Source: https://www.concretepage.com/angular/angular-title-service-and-canonical-url

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!