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"]
Related
I am trying to write an expression for AngularJs. I need to use it inside an ng-pattern directive so to put a validation constraint to a form.
What I actually need is a regex for a URL in https that has always to end with a slash: /.
It would be nice if it ends more specifically in /pre/last/
How do I solve this problem?
This RegEx might help us to match /pre/last/. It creates two groups, in case we wish to call those groups, we can do so using $2 for /pre/last/ and $1 for first the part of our URL (Please see the image).
'/(.+)(\/pre\/last\/)/g'
We might not want to bound it with start (^) or end ($) anchors, and it might still does our desired matchings.
This post explains how we would do so in JavaScript.
Maybe this help
/.*\/pre\/last\/$/g
what it basically match is any string that ends with /pre/last/
In my angular application, I need to make GET call to a Tomcat server. This GET call requires query parameters which could contain special characters too like "+", "/", "/+"
GET call is being made from angular controller using $window.open with target as "_blank"
Currently the redirection is getting failed without any encoding.
So, I added encoding in .js file before the GET call is being made by using encodeURIComponent.
Then I added decoding logic using URLDecode.decode in backend java code to decode query parameters.
But still it doesn't work.
It works only if I encode query parameters twice within the .js file using encodeURIComponent twice.
I am trying to find the root cause for double encoding but no luck yet. I would greatly appreciate if anyone could share any inputs.
Made it work by adding a * in path parameter in app.js. Adding a star means that the request will include multiple path parameters separated by /, and so angular will not try to encode / in the request.
Double encoding could also work but then the server side logic has to be modified to decode the request parameters twice and replace %2B2F by %2F
I am using angularjs router, as the application development is almost done, I cant use UI-router. Recently I implemented two optional parameter in route by following this answer.
Here is what I did.
app.when('/someUrl/:param1?/:param2?',{
templateUrl:'templateurl',
controller:'controllerName'
});
But when I use $location.path('/someUrl/1234/5678');, the url is adding equivalent hex code of '?' in URL either parameter is available or not.
I am not sure why this parameter is coming even if I am sending parameter.
the url is looking like
localhost/someurl/1234%3F/5638%3F
How can avoid this %3F and keep optional routing functionality without using duplicate route definitions.
**Sorry for typo mistake, I already defined routes with :, that is not problem with :.
%3F is ?, Since you have not provided the : its treated as part of URL thus they are encoded.
You need to use : to define parameter.
app.when('/someUrl/:param1?/:param2?',{
templateUrl:'templateurl',
controller:'controllerName'
});
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.
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.