1

My angular app with ui-router is running inside a page at /content/page.html. The page also has a

<base href="/somewhere"> 

tag in the head.

Due to the base tag I end up with incorrect URLs pointing to /somewhere#/section1 instead of /content/page.html#/section1

I'm unable to remove this base tag since I'm just a small part of a large site. Is there a way to make ui-router generate absolute URLs by default? I've noticed that ui-sref-opts="{absolute: true}" can do this, but it generates the link using the base tag. Is there a way to make ui-router always create absolute URLs using window.location.pathname?

edit: I've created this plunker that demonstrates the problem.

Regards, Markus

Markus Kramer
  • 411
  • 5
  • 13

3 Answers3

0

You can try to replace <base href="/somewhere"> by <base href="/"> and add a link with your url in your config route in app.js.

html:

<base href='/'>
<a ui-sref="nameOfTheRoute">Link</a>

in app.js add the route:

$stateProvider.state('nameOfTheRoute',{url:'/', templateUrl:'Content/page.html'})

PS: I tried this on my application with another base href and it's works so you can try without change base url tag.

html:

<base href='/somewhere'>
<a ui-sref="nameOfTheRoute">Link</a>

in app.js add the route:

$stateProvider.state('nameOfTheRoute',{url:'/somewhere', templateUrl:'Content/page.html'})

I hope this will help you.

JonathanTheBrosh
  • 208
  • 3
  • 14
  • Thx. It seems it would not be possible for users to right click the link and open it in a new tab, wouldn't it? The angular app and ui-router are not available at '/somewhere'. I haven't tried it yet. – Markus Kramer Aug 13 '15 at 07:41
  • Hi, have you add the dependancy in your index to use ui router in your app.js ? The right click is mandatory ? For me, I use Modal bootstrap to open my view in new window. But I don't know if this can correspond with yours expectations. – JonathanTheBrosh Aug 13 '15 at 08:19
  • What technology you use for your application ? – JonathanTheBrosh Aug 13 '15 at 08:23
  • To open a new tab look this link: http://stackoverflow.com/questions/21519113/angularjs-open-a-new-browser-window-yet-still-retain-scope-and-controller-and . To the pop menu you can look with bootstrop I think. – JonathanTheBrosh Aug 13 '15 at 08:38
  • I just want to make sure the site has a normal website behavior. That means that links can be opened in new tabs and can be bookmarked. I've created a plunker to demonstate the problem (see above). I don't think the solution you mentioned would solve this. – Markus Kramer Aug 13 '15 at 13:00
0

I hope this answer help you more than my previous answers ^^. I tested your plunker and now the default route works. I hope this is what you search

new plunker

Keep me informed.
JonathanTheBrosh
  • 208
  • 3
  • 14
  • Thanks for your help! The links in this plunker still point to http://run.plnkr.co/somewher/route2 though. Tested with Chrome. – Markus Kramer Aug 17 '15 at 06:40
  • Yes, I changed the route to verify if the redirect works. So, your problem is resolved ? – JonathanTheBrosh Aug 17 '15 at 08:53
  • Why the link is wrong ? Your route contain somewhere but you can anyway redirect to your page html in /content/page.html or I realy don't understood your problem. Can you explain more clearly your problem in this case. – JonathanTheBrosh Aug 18 '15 at 09:56
  • The link points to http://run.plnkr.co/somewher/route1. This is wrong. It should be http://run.plnkr.co/route1. – Markus Kramer Aug 18 '15 at 15:19
  • And Why you can't change for . Because, I tried it and when the href is equal to "/" the link is good (http://run.plnkr.co/route1) – JonathanTheBrosh Aug 18 '15 at 15:31
-3

Solved it by using an angular decorator and overriding the behaviour of the $urlRouter 'href' function.

Markus Kramer
  • 411
  • 5
  • 13