in AngularJS can I set up a style route like this:
http://www.mydomain.com/title-and-more-irrelevant-text-123456-category.html
where '123546' is an indentifier ?
with:
$locationProvider.html5Mode(true);
I must make this because I need keep the old URLs
If this is the entire path, then I guess it will be hard because it is too generic.
If however you can live with paths in the form /prefix/title-and-stuff-123456-foo.html (i.e. the entire URL would look like http://mysrv.com/ctx/#/prefix/title-and-stuff-123.html), read on.
You can define the route as: /prefix/*titleAndId. Notice the *. Then the entire string is accessible inside any controller as $route.current.params.titleAndId. You can parse this string (using an appropriate regular expression perhaps) and extract any information, then load the appropriate content.
The * in the route definition enables the argument to contain slashes too. If slashes are not required, you can use /prefix/:titleAndId and use it the same way.
Related
The default express router uses : (colon) to mark a parameter e.g.
users/:id
But when I was using other (non node.js) framework, I have noticed that they use curly brackets, e.g.
users/{id}
And I prefer the second style because browser escapes curly brackets unlike colon (Which I want to use in my URL), and, I just like it.
So, the question is, can I override default express route URL handler?
Unfortunately you cant. The Express.js framework relies on path-to-regexp library for extracting params from urls. And its usage is hardcoded inside the codebase, so you don't have any extension points to override that behavior.
The question is pretty self explanatory. I basically use '/' in a value on the query string of my URL, and angular (or the browser?) encodes it as '%2F'. Normally I wouldn't fight this, but I've read that '/' isn't fully restricted for use on the query string portion, and since this URL will be shared, it would be a lot prettier with '/' in place of '%2f'. Do you of any way to achieve this? Im using UI-router and the URL's are thus being formatted interally (i.e. I'm not setting $location.url()).
Edit: I can show you some code but there's nothing really descriptive on my end. Lets say I have
var queryVal = some/place/tobe;
And a state defined using ui-router with a url such as
url: /:foo/bar?q
Then I'm calling
$state.go('state', params);
Where params is an object with the proper parameters, including the unencoded queryVal.
The problem is that somewhere down the line angular or the browser is automatically turning
some/place/tobe
into
some%2Fplace%2Ftobe
And that encoded version is what shows up in the address bar.
I have a promo block, that contains several images and links. Some of them are leading to my site, some to external resources. Currently, i use this piece of code for this links.
<a {{ bindAttr href="link" }}><img {{ bindAttr src="image" }} /></a>
However, this actually reloads a page, and i don't want that in case i'm navigating inside my site. Also, it could mean breaking my app if we encounter some non-existent routes, in case of typos or whatever.
So, what i'm trying to do is to add an action, that checks if the route exists and then do a proper transitionTo, and if the route doesn't exist do some sort of default fallback, but i don't know how to make this check. Have anyone did something similar already?
P.S. I know that transitionTo could accept urls as a parameter, but Ember docs say
It is also possible to pass a URL (a string that starts with a /).
This is intended for testing and debugging purposes
and should rarely be used in production code.
And doesn't help with preventing transition if the route doesn't exist.
You could have a default route that would be a 'catch-all' which you would specify in your router as something like:
this.route('badRoute', {path: '/*pathname' });
at the bottom after you have declared all your routes. When none of your routes match, this will be matched and you can redirect or whatever when it transitions to that route.
I am using hash-based navigation in my AngularJS app rooted at /.
If a user navigates to my app like this:
http://example.com/?foo
A moment after the page loads, something (possibly Angular) is causing the address bar to look different than I expected.
What I saw:
http://example.com/?foo#/?foo
What I expected to see:
http://example.com/?foo#/
Why is this happening, and can I turn it off?
I'd wager you need to be in 'html5 mode' to not have the hash fragment... though I'm uncertain.
http://docs.angularjs.org/guide/dev_guide.services.$location
$locationProvider.html5Mode(true).hashPrefix('!');
In your app configuration, you can mess with that config param and it'd probably get rid of it.
This appears to be duplicating the hash with the path.
Check out the $location service. It has both path() and hash() methods. The second, duplicated part is the hash, the first part is the path.
Unless you are using html5 mode, all of Angular's part of the URL appears in the fragment. The problem is that Angular doesn't know about the base part of the URL (perhaps the ?foo was needed just to get Angular to load) so it won't attempt to manipulate it, it just puts its own stuff on as a fragment.
I suggest the best thing would be to check $window.location.search for a query string, and if you find one do the redirect to the URL you actually want yourself. You'll still need to do that redirect by assigning to $window.location rather than $location and it will force your angular app to reload but at least you'll end up where you want to get to.
Alternatively you could reconfigure your web server to make the appropriate rewrite, but you may not want to or be able to do that.
Or you tell your users to only use URLs they got from the app, not to try to make them up for themselves.
I'm learning Sammy.js and using it to build a mini-application on top of a REST API I'm working on. I couldn't find this immediately, and I am probably missing something.
I'm using routes like:
this.get("/databases/:name", function () { ... });
I basically want to take the route path and pass it along to my REST API, since they largely match up anyways. I've inspected this within the callback, and found a property called path that contains the full path, including the filename. (i.e. /index.html#/databases/foo)
All I care about is what comes after the #, and I wonder if there is something baked in so I don't have to use this.path.split("#")[1].
You could just look at window.location.hash and strip off the leading #. That should be pretty much the same as pulling information out of this.path.