Skip to main content
added 550 characters in body
Source Link
Sami
  • 8.5k
  • 9
  • 72
  • 104

Edit You can use camel case. See it used at end part of answer

this.title = camelize(lastPartOfUrl.replace(/-/g,' ')); function camelize(str) { return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) { if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces return index == 0 ? match.toLowerCase() : match.toUpperCase(); }); } 

Answer is the using some logic/trick.

Proof with simple javascript (a must working solution)

 this._router.events.subscribe((event) => { console.log('route changed'); var url = window.location.toString(); var ar = url.split('/'); var lastPartOfUrl = ar[ar.length-1]; this.title = "Title "+lastPartOfUrl ; }); 

With angular path (only if you don't like the above simple way).. Now same logic but only if .path() is working correctly for you

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); this.title = "Title "+lastPartOfUrl ; }); 

Edit

ItAbove will give you Title 1. And if you really want dynamic titles then you must have to follow some pattern off course. Either you have to use if or you have to use some formula/pattern

Say your current url is http://stackoverflow.com/questions/38613960/angular2-using-router-subscribe-to-watch-url-change . You can get the best of title from that url could be like

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); //this.title = lastPartOfUrl.replace(/-/g,' '); //following will be the result //angular2 using router subscribe to watch url change  this.title = camelize(lastPartOfUrl.replace(/-/g,' ')); //Angular2 Using Router Subscribe To Watch Url Change  }); 

Answer is the using some logic/trick.

Proof with simple javascript (a must working solution)

 this._router.events.subscribe((event) => { console.log('route changed'); var url = window.location.toString(); var ar = url.split('/'); var lastPartOfUrl = ar[ar.length-1]; this.title = "Title "+lastPartOfUrl ; }); 

With angular path (only if you don't like the above simple way).. Now same logic but only if .path() is working correctly for you

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); this.title = "Title "+lastPartOfUrl ; }); 

Edit

It will give you Title 1. And if you really want dynamic titles then you must have to follow some pattern off course. Either you have to use if or you have to use some formula/pattern

Say your current url is http://stackoverflow.com/questions/38613960/angular2-using-router-subscribe-to-watch-url-change . You can get the best of title from that url could be like

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); this.title = lastPartOfUrl.replace(/-/g,' '); //following will be the result //angular2 using router subscribe to watch url change }); 

Edit You can use camel case. See it used at end part of answer

this.title = camelize(lastPartOfUrl.replace(/-/g,' ')); function camelize(str) { return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) { if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces return index == 0 ? match.toLowerCase() : match.toUpperCase(); }); } 

Answer is the using some logic/trick.

Proof with simple javascript (a must working solution)

 this._router.events.subscribe((event) => { console.log('route changed'); var url = window.location.toString(); var ar = url.split('/'); var lastPartOfUrl = ar[ar.length-1]; this.title = "Title "+lastPartOfUrl ; }); 

With angular path (only if you don't like the above simple way).. Now same logic but only if .path() is working correctly for you

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); this.title = "Title "+lastPartOfUrl ; }); 

Edit

Above will give you Title 1. And if you really want dynamic titles then you must have to follow some pattern off course. Either you have to use if or you have to use some formula/pattern

Say your current url is http://stackoverflow.com/questions/38613960/angular2-using-router-subscribe-to-watch-url-change . You can get the best of title from that url could be like

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); //this.title = lastPartOfUrl.replace(/-/g,' '); //following will be the result //angular2 using router subscribe to watch url change  this.title = camelize(lastPartOfUrl.replace(/-/g,' ')); //Angular2 Using Router Subscribe To Watch Url Change  }); 
added 2 characters in body
Source Link
Sami
  • 8.5k
  • 9
  • 72
  • 104

Answer is the using some logic/trick.

Proof with simple javascript (a must working solution)

 this._router.events.subscribe((event) => { console.log('route changed'); var url = window.location.toString(); var ar = url.split('/'); var lastPartOfUrl = ar[ar.length-1]; this.title = "Title "+lastPartOfUrl ;  }); 

With angular path (only if you don't like the above simple way).. Now same logic but only if .path() is working correctly for you

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); this.title = "Title "+lastPartOfUrl ; }); 

Edit

It will give you Title 1. And if you really want dynamic titles then you must have to follow some pattern off course. Either you have to use if or you have to use some formula/pattern

Say your current url is http://stackoverflow.com/questions/38613960/angular2-using-router-subscribe-to-watch-url-change . You can get the best of title from that url could be like

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path();  this.title = lastPartOfUrl.replace(/-/g,' '); //following will be the result //angular2 using router subscribe to watch url change }); 

Answer is the using some logic/trick.

Proof with simple javascript (a must working solution)

 this._router.events.subscribe((event) => { console.log('route changed'); var url = window.location.toString(); var ar = url.split('/'); var lastPartOfUrl = ar[ar.length-1]; this.title = "Title "+lastPartOfUrl ; }); 

With angular path (only if you don't like the above simple way).. Now same logic but only if .path() is working correctly for you

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); this.title = "Title "+lastPartOfUrl ; }); 

Answer is the using some logic/trick.

Proof with simple javascript (a must working solution)

 this._router.events.subscribe((event) => { console.log('route changed'); var url = window.location.toString(); var ar = url.split('/'); var lastPartOfUrl = ar[ar.length-1]; this.title = "Title "+lastPartOfUrl ;  }); 

With angular path (only if you don't like the above simple way).. Now same logic but only if .path() is working correctly for you

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); this.title = "Title "+lastPartOfUrl ; }); 

Edit

It will give you Title 1. And if you really want dynamic titles then you must have to follow some pattern off course. Either you have to use if or you have to use some formula/pattern

Say your current url is http://stackoverflow.com/questions/38613960/angular2-using-router-subscribe-to-watch-url-change . You can get the best of title from that url could be like

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path();  this.title = lastPartOfUrl.replace(/-/g,' '); //following will be the result //angular2 using router subscribe to watch url change }); 
added 2 characters in body
Source Link
Sami
  • 8.5k
  • 9
  • 72
  • 104

Answer is the using some logic/trick.

Proof with simple javascript (a must working solution)

 this._router.events.subscribe((event) => { console.log('route changed'); var url = window.location.toString(); var ar = url.split('/'); var lastPartOfUrl = ar[ar.length-1]; this.title = "Title"+lastPartOfUrl"Title "+lastPartOfUrl ; }); 

With angular path (only if you don't like the above simple way).. Now same logic but only if .path() is working correctly for you

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); this.title = "Title"+lastPartOfUrl"Title "+lastPartOfUrl ; }); 

Answer is the using some logic/trick.

Proof with simple javascript (a must working solution)

 this._router.events.subscribe((event) => { console.log('route changed'); var url = window.location.toString(); var ar = url.split('/'); var lastPartOfUrl = ar[ar.length-1]; this.title = "Title"+lastPartOfUrl ; }); 

With angular path (only if you don't like the above simple way).. Now same logic but only if .path() is working correctly for you

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); this.title = "Title"+lastPartOfUrl ; }); 

Answer is the using some logic/trick.

Proof with simple javascript (a must working solution)

 this._router.events.subscribe((event) => { console.log('route changed'); var url = window.location.toString(); var ar = url.split('/'); var lastPartOfUrl = ar[ar.length-1]; this.title = "Title "+lastPartOfUrl ; }); 

With angular path (only if you don't like the above simple way).. Now same logic but only if .path() is working correctly for you

 this._router.events.subscribe((event) => { console.log('route changed'); var lastPartOfUrl = this.location.path(); this.title = "Title "+lastPartOfUrl ; }); 
Source Link
Sami
  • 8.5k
  • 9
  • 72
  • 104
Loading