Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetTitle programmatically #7

Closed
martinop opened this issue Sep 9, 2016 · 6 comments
Closed

SetTitle programmatically #7

martinop opened this issue Sep 9, 2016 · 6 comments
Labels
Milestone

Comments

@martinop
Copy link

martinop commented Sep 9, 2016

Why this doesnt work? Just the tags works. rc5

@bergben
Copy link
Contributor

bergben commented Sep 21, 2016

Hey @martinop .
Can you elaborate on what exaclty isn't working?

To set the document title in the component you can just use the Angular Title service:
https://angular.io/docs/ts/latest/cookbook/set-document-title.html

@vinaygopinath vinaygopinath added this to the 2.0 milestone Sep 22, 2016
@vinaygopinath
Copy link
Owner

@martinop I've just published v2.0.0 on npm. Can you use that version and report back here if you're still running into issues with setTitle? Thanks.

@rybaczewa
Copy link

rybaczewa commented Oct 20, 2016

I have the same problem with v2.0.1 (installed from github due to #13). I am using angular2 and ng2-meta with webpack.

Using setTitle in AppComponent works but is instantly overriten by either title from route meta or default title from MetaConfig (depending on whether meta title is provided for route).

export class AppComponent {
  constructor(private metaService: MetaService) {
    // This works but is instantly overriten
    this.metaService.setTitle('test');
  }
}

Using setTitle on other any of route components does nothing. Using setTag is fine.

export class ObjDetailComponent implements OnInit {
  constructor(
    private metaService: MetaService
  ) {}

  ngOnInit(): void {
    // this changes nothing
    this.metaService.setTitle('test');

    // this works as intended
    this.metaService.setTag('og:image', '...');
  }
}

@vinaygopinath
Copy link
Owner

vinaygopinath commented Oct 20, 2016

Hey @rybaczewa, I'm aware of this possibility. A component's constructor is called (and metaService.setTitle within it) before the "route change completed" router event (when the route meta and/or default values are used for the title/other meta tags).

I intended for setTitle to be used in an asynchronous way, possibly after an HTTP request (as in here). I didn't consider that setTitle could be used in conjunction with route meta/defaults. To set the title (and other tags that might be in collision with route meta/defaults) programmatically after the route change completed event, use setTimeout

setTimeout(() => {
  this.metaService.setTitle('test');
});

Meanwhile, I'll look into making sure that programmatic changes have precedence over route meta and default values.

@rybaczewa
Copy link

Oh, I see. I assumed that setTitle at respects MetaConfig defaults, especially title suffix (so there's no need for suffix to be set every time I use setTitle). Thanks for the info though.

@shabbir-dhangot
Copy link

@vinaygopinath Is it ok to use in setTimeout(). Is it a proper way for SEO ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants