You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
@gdi2290 What is the status here? Does it make sense to have so many different things in one issue? As we discussed, we should create a new module under Universal with all the common services people will need. I am modifying the title for this issue to reflect that.
@megalepozy I agree with this. We should have one of the universal services that automatically sets the canonical link in the head. Also, related is i18n href lang tags.
As posted by @Quixomatic, the workaround for now is:
import { Injectable, Inject, ElementRef, Renderer } from '@angular/core'
import { DOCUMENT } from '@angular/platform-browser'
@Injectable()
export class InterfaceService {
//--------------------------------------------
//! Set the variables to their default values
//--------------------------------------------
constructor (@Inject(DOCUMENT) private document) {
}
//----------------------------
//! Set the title of the page
//----------------------------
setTitle (title: string) {
this.document.title = title
}
}
It will still be good to create these universal services, but this workaround is pretty simple and should cover setting the title.
Reacted by megalepozy, Scott Wyatt, Julien Capoulun and Matti Järvinen
Not sure if still of interest, but putting together all suggestions found here and around, I've come up with a solution that is working well for me:
import { Component, AfterViewInit, Renderer, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
constructor(@Inject(DOCUMENT) private document: any, private renderer: Renderer) {}
ngAfterViewInit(): void {
let headChildren = this.document.head.children;
for (let i = 0; i < headChildren.length; i++) {
let element = headChildren[i];
if(element.name === 'title') this.renderer.setText(element, 'App title');
if(element.name === 'meta' && element.attribs.translatestring) this.renderer.setElementAttribute(element, 'content', 'I am a translated string');
}
// To add new meta
const elem = this.renderer.createElement(this.document.head, 'meta');
this.renderer.setElementProperty(elem, 'name', 'foo');
}
}
If you need this also client side you can simply transform this into a service, and then call it each time the route changes, then use this.renderer.setElementAttribute to change the attribute content to the right strings (e.g. from a constant with all the values based on the route).
Reacted by KyeongTae Kim, Ivan Matiishyn and Olivier Combe
@ghego1 i tried to use your for loop to set meta tags in the InterfaceServer
import { Injectable, Inject, ElementRef, Renderer } from '@angular/core'
import { DOCUMENT } from '@angular/platform-browser'
@Injectable()
export class InterfaceService {
//--------------------------------------------
//! Set the variables to their default values
//--------------------------------------------
constructor (@Inject(DOCUMENT) private document) {
}
//----------------------------
//! Set the title of the page
//----------------------------
setTitle (title: string) {
this.document.title = title
}
}
But im gettings this error: "No provider for ElementRef!", can you help me?
@DBuit, I think the problem is that you are doing this in a Service. I implemented this in a component, and then passed to the service the rendered object.
I have not yet studied how Angular2 works behind the scenes so I'm unable to provide you a full description of why this can't be delegated entirely to a Service, but I can share with you the following working code :-)
hi @ilDon, your solution does not work when we open view page source angular (SSE), can you please give support with angular SSE as well,
i am using this for adding canonical URl
angular-automatic-lock-bot commented on Sep 4, 2019
Activity
PatrickJS commentedon Mar 9, 2016
we're waiting for the next next release
PatrickJS commentedon Apr 15, 2016
<universal-styles>was addedPatrickJS commentedon Apr 20, 2016
I'm adding NgPreboot
jeffwhelpley commentedon May 6, 2016
@gdi2290 What is the status here? Does it make sense to have so many different things in one issue? As we discussed, we should create a new module under Universal with all the common services people will need. I am modifying the title for this issue to reflect that.
[-]create index.html directives[/-][+]create universal services[/+]megalepozy commentedon Jun 7, 2016
I'm not sure if it's the right place to ask for it but is there something like NgLink for setting canonical links? it's badly needed for SEO
jeffwhelpley commentedon Jun 8, 2016
@megalepozy I agree with this. We should have one of the universal services that automatically sets the canonical link in the head. Also, related is i18n href lang tags.
jeffwhelpley commentedon Oct 5, 2016
As posted by @Quixomatic, the workaround for now is:
It will still be good to create these universal services, but this workaround is pretty simple and should cover setting the title.
10 remaining items
threesquared commentedon Oct 12, 2016
As a pretty hacky solution I mentioned above you can access
getDOM()as I have done here which works at the moment...PatrickJS commentedon Oct 27, 2016
closing as
<html>component isn't supported by angular anymorethreesquared commentedon Oct 27, 2016
@gdi2290 @jeffwhelpley mentioned above that this thread would be updated with details of a meta tag service?
samvloeberghs commentedon Nov 15, 2016
@threesquared do you mind if I use your implementation for now? Works like a charm :)
PatrickJS commentedon Nov 16, 2016
@threesquared please see PR angular/angular#12322 for more details on the Meta Service
threesquared commentedon Nov 16, 2016
@gdi2290 thanks!
@samvloeberghs of course.
marklonquist commentedon Nov 30, 2016
The suggestion in #309 (comment) did not work for me for setting the title serverside. I'm getting a 'data' is undefined error.
Works fine if I use it client-side.
ilDon commentedon Jan 7, 2017
Not sure if still of interest, but putting together all suggestions found here and around, I've come up with a solution that is working well for me:
If you need this also client side you can simply transform this into a service, and then call it each time the route changes, then use
this.renderer.setElementAttributeto change the attributecontentto the right strings (e.g. from a constant with all the values based on the route).DBuit commentedon Feb 1, 2017
@ghego1 i tried to use your for loop to set meta tags in the InterfaceServer
But im gettings this error: "No provider for ElementRef!", can you help me?
ilDon commentedon Feb 1, 2017
@DBuit, I think the problem is that you are doing this in a Service. I implemented this in a component, and then passed to the service the rendered object.
I have not yet studied how Angular2 works behind the scenes so I'm unable to provide you a full description of why this can't be delegated entirely to a Service, but I can share with you the following working code :-)
Component:
And then the service:
jrmcdona commentedon Aug 1, 2017
Does this work well with dyanmic link and script tags from an external service?
raviguru123 commentedon Nov 17, 2018
hi @ilDon, your solution does not work when we open view page source angular (SSE), can you please give support with angular SSE as well,
i am using this for adding canonical URl
angular-automatic-lock-bot commentedon Sep 4, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.