-
Notifications
You must be signed in to change notification settings - Fork 27.5k
add $locationProvider.baseHref() #2805
Comments
👍 yeap! |
Would simplify my life and save us quite a few lines of code. +1 |
👍 |
You know you can add <base href="/myroot/" target="_blank"/> to |
that reference was in error, please ignore |
A workaround I found for this was to redefine the behaviour of the // override the $browser.baseHref() method so it returns "/" without needing to use a <base href="/"> element
// in the head of the document (which causes problems with SVG patterns amongst other things)
$browser.baseHref = function() { return "/" }; |
@greglockwood I like that solution for setting base href asssuming you can get angular loaded with a correct path first ;) I'm hoping to put that to use tomorrow! Correct me if I'm wrong -- is this issue about making angular apps that don't need to be tweaked when their actual root path changes? I'm still really looking forward to some sort of solution to being able to deploy apps in subdirectories without modifying their code. |
@stu-salsbury I don't believe you are right in saying that this issue is about having Angular apps working in subdirectories without modifying lines of code. This issue is about being able to specify in code what is currently pulled from the One way I have come up with in the past to get around an application being able to be deployed in a subdirectory or not and still work is to determine the base URL from the full URL, since you can subtract any known relative path from the whole URL. You could combine that trick with either my workaround above, or if this feature gets added, with the official function call to have this kind of "deploy anywhere" functionality. Let me illustrate what I meant above in more concrete terms. Let's say I have all my application routes starting with |
The html still needs to reference js, images etc with relative paths, and if the base href in the file is incorrect, then those references won't work. Making the references be absolute makes putting apps in subdirs broken again. So I think this can't be solved with something added to $locationProvider, since Angular starts sometime after the document is loaded. Right? |
@wmertens correct. For asset paths in HTML files (paths in CSS are ok, they're loaded relative to the CSS file) you'd still need to prefix your paths with the root if you have a variable root. |
we dove into this yesterday with @geddski and we have a plan to implement ngAppRoot directive which will take in a string that represents url prefix that should be used internally for $location, $http and $route urls. with this and some sort of serverside templating (signified by
All of your urls in javascript will then remain absolute-root urls (e.g. |
Would |
Thought about base, but don't want to mislead people into thinking it
|
closing in favor of #3102 |
correct. the resolution rules for base[href] and "root" are very different, so we want to make the difference clear. for example if base[href]="/foo/bar" and url is "/baz" then the final url is still "/baz". however if root is "/foo/bar" then the final url is "/foo/bar/baz". |
..So is this resolved? For those of trying to make sense of this, it would be helpful if someone provided an example of what we are supposed to do. Currently it is clear as mud. Thanks in advance |
The problem I'm having is I need to give the users a "share" button, which should generate a full URL to one of my routes: if the app is running in a subdirectory
or if the app has its own domain
So to attempt to construct this dynamically, I try this:
The problem is, how do I know if angular is running in a subpath? Where can I get the value for that from? It'd be nice to just be able to do something like
Then I could easily generate URLs for things like "share buttons". |
For those who still search for a solution, you can use $window.location.pathname. |
Running an Angular app in a subdirectory is painful. All your routes, resources, locations, etc etc have to be manually prefixed with the application context. Example:
It's annoying and also error prone if your development env runs in a subdirectory (pretty common).
One current solution is to add a
<base href="/myroot/">
tag to the document. This solves the problems from the Angular perspective but causes many issues of its own, especially for third-party libs/widgets that use anchors.The ideal would be to make $location configurable with a base path, much like Backbone router's
root
config.This change would make running Angular in different contexts much easier.
The text was updated successfully, but these errors were encountered: