Redirect AngularJS up one directory - angularjs

Im using Angular.js and I have several routes:
http://localhost/tool/new
http://localhost/tool/report/#/59FD59D56F20DDFA722F5B31796CAE3396C
tool/new is the form where a user would create a new report using the tool. The program then runs in the background and the user can access the report by going to tool/report/#/their-id where their-id is a big SHA1 hash.
That hash is a route param, if that hash is invalid I need to redirect the user back to tool/new. The problem Im having is that the program file structure is:
tool/new/index.html // The create new report form
tool/report/index.html // The report area
I've been trying to use $location to redirect like this:
$location.path('/new');
But it just redirects to tool/report/#/new and that doesn't help.
Does anyone know how to tell Angular to move up one directory and redirect?
Based on the answer from Branimir, I get the URL and trim it at the current (report) directory. I do this because the tool name and domain can change depending on the server its located on. This worked for me.
var dialog = $dialogs.notify('We were unable to find your tool run.','<p>It is possible that the URL you have entered is incorrect or that your tool run has been deleted due to inactivity.</p><p>Please confirm your URL and try again; or create a new tool run.</p>');
dialog.result.then(function(btn){
var url = $location.absUrl();
var base = url.substring(0,url.indexOf('/report/'));
window.location = base + '/#/new';
},function(btn){});
Note Im using $dialogs from here https://github.com/m-e-conroy/angular-dialog-service/blob/master/dialogs.min.js to tell the user the id is bad.

If you want to reload page use:
$window.location.href = 'http://localhost/tool/new';
$location service doesn't reload a page, it changes a URL in the same AngularJS application.

Related

Angular state navigation - add querystring to URL

My application when starts can automatically navigate to a specified state like this:
localhost://start.html#/case
When a user navigates to this state some parameters are prepared internally to get specific data for templates shown in case state. I need to be able to have an URL with querystring added to the URL in order for the user to copy and paste it into his email or somewhere else. Something like this:
localhost://start.html?project=1234#/case
I have spent a considerable time trying to make it work. I tried
$location.search('project', '1234');
But could not get an URL that would actually work.
Please help me with this task.
Thanks
Hi you can add the parameters at the end of the url
localhost://start.html#/case?project=1234
then you can get the parameters in angular using $location
var projectId = $location.search().project;
// or
var projectId = $location.search('project');
// clear url
$location.search('project', null);

Generating random URL in AngularJS

I wish to see if it is possible to generate a random link each time an admin visits the admin page.
for example: instead of having: "".com/admin, there would be "".com/a93k, "".com/9dik. The page stays the same but just so the end-user can't access the page from the address bar.
Thanks as I am new and do not know how I can implement this.
In .config of your app put something like
window.adminHash = Math.random().toString(36).substring(2,6);
In your routing
url: '/' + window.adminHash
Then you can use "window.adminHash" if you want to redirect to your route, which will change every time you refresh the page
You can add www.test-example.com/admin/{provide some ID}
And If there isn't ID provide discard that URL, if it is provided you can make discrimination.

AngularJS: How to clear query parameters in the URL?

My AngularJS application needs to have access to the user's LinkedIn profile. In order to do that I need to redirect the user to a LinkedIn URL which contains a callback redirect_uri parameter which will tell LinkedIn to redirect the user back to my webapp and include a "code" query param in the URL. It's a traditional Oauth 2.0 flow.
Everything works great except that LinkedIn redirects the user back to the following URL:
http://localhost:8080/?code=XXX&state=YYY#/users/123/providers/LinkedIn/social-sites
I would like to remove ?code=XXX&state=YYY from the URL in order to make it clean. The user does not need to see the query parameters I received from LinkedIn redirect.
I tried $location.absUrl($location.path() + $location.hash()).replace(), but it keep the query params in the URL.
I am also unable to extract the query parameters, e.g. "code", using ($location.search()).code.
It seems like having ? before # in the URL above is tricking Angular.
I use
$location.search('key', null)
As this not only deletes my key but removes it from the visibility on the URL.
I ended up getting the answer from AngularJS forum. See this thread for details
The link is to a Google Groups thread, which is difficult to read and doesn't provide a clear answer. To remove URL parameters use
$location.url($location.path());
To remove ALL query parameters, do:
$location.search({});
To remove ONE particular query parameter, do:
$location.search('myQueryParam', null);
To clear an item delete it and call $$compose
if ($location.$$search.yourKey) {
delete $location.$$search.yourKey;
$location.$$compose();
}
derived from angularjs source : https://github.com/angular/angular.js/blob/c77b2bcca36cf199478b8fb651972a1f650f646b/src/ng/location.js#L419-L443
You can delete a specific query parameter by using:
delete $location.$$search.nameOfParameter;
Or you can clear all the query params by setting search to an empty object:
$location.$$search = {};
At the time of writing, and as previously mentioned by #Bosh, html5mode must be true in order to be able to set $location.search() and have it be reflected back into the window’s visual URL.
See https://github.com/angular/angular.js/issues/1521 for more info.
But if html5mode is true you can easily clear the URL’s query string with:
$location.search('');
or
$location.search({});
This will also alter the window’s visual URL.
(Tested in AngularJS version 1.3.0-rc.1 with html5Mode(true).)
Need to make it work when html5mode = false?
All of the other answers work only when Angular's html5mode is true. If you're working outside of html5mode, then $location refers only to the "fake" location that lives in your hash -- and so $location.search can't see/edit/fix the actual page's search params.
Here's a workaround, to be inserted in the HTML of the page before angular loads:
<script>
if (window.location.search.match("code=")){
var newHash = "/after-auth" + window.location.search;
if (window.history.replaceState){
window.history.replaceState( {}, "", window.location.toString().replace(window.location.search, ""));
}
window.location.hash = newHash;
}
</script>
If you want to move to another URL and clear the query parameters just use:
$location.path('/my/path').search({});
Just use
$location.url();
Instead of
$location.path();
If you are using routes parameters just clear $routeParams
$routeParams= null;
How about just setting the location hash to null
$location.hash(null);
if you process the parameters immediately and then move to the next page, you can put a question mark on the end of the new location.
for example, if you would have done
$location.path('/nextPage');
you can do this instead:
$location.path('/nextPage?');
I've tried the above answers but could not get them to work. The only code that worked for me was $window.location.search = ''
I can replace all query parameters with this single line: $location.search({});
Easy to understand and easy way to clear them out.
The accepted answer worked for me, but I needed to dig a little deeper to fix the problems with the back button.
What I noticed is that if I link to a page using <a ui-sref="page({x: 1})">, then remove the query string using $location.search('x', null), I don't get an extra entry in my browser history, so the back button takes me back to where I started. Although I feel like this is wrong because I don't think that Angular should automatically remove this history entry for me, this is actually the desired behaviour for my particular use-case.
The problem is that if I link to the page using <a href="/page/?x=1"> instead, then remove the query string in the same way, I do get an extra entry in my browser history, so I have to click the back button twice to get back to where I started. This is inconsistent behaviour, but actually this seems more correct.
I can easily fix the problem with href links by using $location.search('x', null).replace(), but then this breaks the page when you land on it via a ui-sref link, so this is no good.
After a lot of fiddling around, this is the fix I came up with:
In my app's run function I added this:
$rootScope.$on('$locationChangeSuccess', function () {
$rootScope.locationPath = $location.path();
});
Then I use this code to remove the query string parameter:
$location.search('x', null);
if ($location.path() === $rootScope.locationPath) {
$location.replace();
}

Backbone.js: How to utilize router.navigate to manipulate browser history?

I am writing something like a registration process containing several steps, and I want to make it a single-page like system so after some studying Backbone.js is my choice.
Every time the user completes the current step they will click on a NEXT button I create and I use the router.navigate method to update the url, as well as loading the content of the next page and doing some fancy transition with javascript.
Result is, URL is updated which the page is not refreshed, giving a smooth user experience. However, when the user clicks on the back button of the browser, the URL gets updated to that of a previous step, but the content stays the same. My question is through what way I can capture such an event and currently load the content of the previous step and present that to the user? Or even better, can I rely on browser cache to load that previously loaded page?
EDIT: in particular, I'm trying something like mentioned in this article.
You should not use route.navigate but let the router decide which form to display based on the current route.
exemple :
a link in your current form of the registration process :
<a href="#form/2" ...
in the router definition :
routes:{
"form/:formNumber" : "gotoForm"
},
gotoForm:function(formNumber){
// the code to display the correct form for the current url based on formNumber
}
and then use Backbone.history.start() to bootstrap routing

Cakephp One login function / multiple login views

I have one website with a login system. However, I would like the login view to be different depending on what link has the user used to get to the login screen.
Something like:
function login ($from_page = null) {
if (isset($page)) $this->render('login_alternate_view');
else $this->render('login'); //default login view
}
And then each of the login views (login.ctp, login_alternate_view.ctp) would have the login form plus other stuff specific to each one.
Is this possible in some way? I've already tried something like the example above but it doesn't work...
So I fixed it using GET variables:
/users/login?some_var=some_value
And then in the login function I catch that variable's value with:
$this->params['url']['some_var'];
This way I can "customize" my login function depending on the link the user uses
First show the real error message you're talking in the comments about and not "something".
I guess that you want the current page url the user is on when he logs in? How to you generate the modal? Request the whole form via ajax or is it embedded in the page you're on? If it's embedded I would put the current page url the user is on in a hidden field "from" in the login form and check that.

Resources