angular.js get slash-separated path from url - angularjs

I need to take as a parameter in my site's urls a file path (that will contain some number of '/' characters). How can I parse such a parameter from a url? Something like http://localhost/path/to/file would be preferable, but if that doesn't work, http://localhost/?path=/path/to/file or something could work as well.

to use location in your controller. first turn html5mode to true like this
Then you can use
location.search and it will return string without any error.
for more use of location go here https://docs.angularjs.org/api/ng/service/$location

Take a look at URI.js, it looks like what you need.

Related

React.js - Avoid automatically decoding HTML entities

My react app seems to be automatically decoding %25 to a %, however I need it to stay as %25.
Let's say I have a route that looks like the below. The route has a param which is a string containing an encoded url.
https://mywebsite.com/{encoded_url_string}
As an example, let's say the unencoded url string is:
https://urlparam.com/public/?test=c%3Dvalue
Which, encoded, would become
https%3A%2F%2Furlparam.com%2Fpublic%2F%3Ftest%3Dc%253Dvalue
So the full route would look like this:
https://mywebsite.com/https%3A%2F%2Furlparam.com%2Fpublic%2F%3Ftest%3Dc%253Dvalue
My issue is that when I grab this param (using the query-string library) from the URL, and try to write it to my page, instead of literally being:
https%3A%2F%2Furlparam.com%2Fpublic%2F%3Ftest%3Dc%253Dvalue
Which is exactly as it is in the URL param, I'm getting:
https%3A%2F%2Furlparam.com%2Fpublic%2F%3Ftest%3Dc%3Dvalue
Notice that the last bit in the first one is "test%3Dc %25 3Dvalue" (ignore the spaces, StackOverflow won't let me bold without it), while the second one is just test%3Dc%3Dvalue (missing the %25)
Any ideas why the %25 is being decoded to a simple %? Any way to prevent that?

Optional route parameters with prefix (angular ui-router)

I need to register to a state, a route where the parameters has a prefix and a slash
(route/to/parPrefix1/:paramValue1/parPrefix2/:paramValue2)
and these parameters are all optional, so if the parameter1 is not set, the resulting url should be:
route/to/parPrefix2/value2
How can I handle this case?
Then I have a second issue:
the date should be specified like this: 20/10/2015
If i solve the first point I could set a parameter per date part, however, is there a more elegant solution?
a real case url: /risultati/destinazioni/:destinations/localita/:locations/partenza/:startingDate/ritorno/:returningDate/adulti/:adults/bambini/:children/eta/:childrenAges/aeroporti/:airports/flessibile/:flexDate/
You most likely need to set up multiple routes, that point to the same view:
ex:
route/to/parPrefix1/:paramValue1
route/to/parPrefix1/:paramValue1/parPrefix2/:paramValue2
I agree with spanndemic, see http://benfoster.io/blog/ui-router-optional-parameters.
Also I think that most of that data should be passed via services or values.
Such a long url will always make things much more difficult.

AngularJS $location get path before hash

I need to extract the path before the # in my AngularJS app, so for example if my app url is:
http://www.domain.com/folder/app/#/home
I need to get only this portion:
http://www.domain.com/folder/app/
I've tried using $location.path, $location.abspath but couldn't find any function that can get me the first portion before the #. Can someone please help me by telling me what I am missing here? and if there is a way to get the first portion of the url (before the #)? Thanks
You could split the path into the pathname and hash parts, and then use the pathname only:
$location.absUrl().split('#')[0]
// "http://www.example.com/folder/app/"
split is just a Javascript string operation returning an array:
'http://www.example.com/folder/app/#/home'.split('#')
["http://www.example.com/folder/app/", "/home"]

HTMLHelper link() added unintended path

I believe this is very simple question. Maybe that's why I can't find it on Google.
When I do this inside View/Product/view.ctp
echo $this->Html->link('Download PDF', 'app/files/product/1/manual.pdf');
The resulting URL is like this:
app/products/app/files/product/1/manual.pdf
It automatically added app/products since this is inside Product's view.
How to nullify that automatic addition?
Thanks
You're specifying a relative url, causing your browser to append the url to the current url.
echo $this->Html->link('Download PDF', '/app/files/product/1/manual.pdf');
(note the leading slash /)
Should result in a link to http://example.com/app/files/product/1/manual.pdf

Encoding url's the Play! Framework

Is there a way to make the Play! Framework ignore slashes and ? in parts of the URL?
Typically, if I have the following:
www.123.com/api/link/http:www.bla.com/?contenId=123&User=test
It won't work. In that case, what I would want to do is simply have the link in the last part of the URL in a String variable to save it. I suppose I can force the client to replace the / and ? by something else, but I would rather keep it simple.
My first thought was that maybe there is a way to configure the routing such that we have something like:
/api/link/{data}
where data would hold whatever remains of the URL. Can't find out how to do that though.
You can't have : / ? except your main URL. You should encode your parameter to append it to main URL. See URLEncoder for Java.
This is not a valid URL:
http://www.123.com/api/link/http://www.bla.com/?contenId=123&User=test
It must be:
http://www.123.com/api/link/http%3a%2f%2fwww.bla.com%2f%3fcontenId%3d123%26User%3dtest
Then you can pass it to {data} parameter and decode it in your handler method.

Resources