0

I have enabled $locationProvider.html5Mode(true).

However I have django as backend. On /logout/ I need to ask the url directly to the server as if $locationProvider.html5Mode(false) or simply visiting: example.com/logout/ and not in the fron-end.

Which angular command should I use to "force" ask the /logout/ url from the server?

Diolor
  • 13,181
  • 30
  • 111
  • 179
  • 2
    One way is to just use window.location = "example.com/logout/" which by-passes AngularJS – LostInComputer Oct 18 '13 at 13:13
  • @LostInComputer can you create me an example with an optimal handler as well (I guess either by `$watch`-ing the `$location` or `$routeProvider`)? Thanks – Diolor Oct 18 '13 at 13:34
  • Why do you need to set the url directly? What are you trying to do? – davekr Oct 18 '13 at 14:38
  • @davekr I have in `urls.py` `url(r'^logout/$', logout, name='logout'),`. The view is: `def logout(request): auth_logout(request);return HttpResponseRedirect('/');`. So I need a way to auth logout the user with that view (I am using [python social auth](https://github.com/omab/python-social-auth) if it matters). – Diolor Oct 18 '13 at 14:42
  • @Diolor So why not to return json with a redirect url from the view and then use it on frontend and change the $location to the url? – davekr Oct 18 '13 at 14:50
  • @davekr example please? – Diolor Oct 18 '13 at 14:55
  • In view: `return HttpResponse(json.dumps({"redirect":"/"}), mimetype='application/javascript')` In JS: `$http.get("logout/").success(function(data) {$location.path(data.redirect)})` – davekr Oct 18 '13 at 15:03

1 Answers1

1

Edit: Turns out that are are two possible solutions:

1) See Telling Angular Js to ignore a specific route

2) Or use $window.location.

Here is an example:

$scope.onLogoutClicked = function() {
    $window.location = "http://google.com";
}

Difference between $window.location and $location

When should I use $location?

Any time your application needs to react to a change in the current URL or if you want to change the current URL in the browser.

What does it not do? It does not cause a full page reload when the browser URL is changed. To reload the page after changing the URL, use the lower-level API, $window.location.href.

Notes:

  • $window is a wrapper for window so that it can be unit tested.
  • $window.location = "URL" and $window.location.href = "URL" does the same thing
Community
  • 1
  • 1
LostInComputer
  • 15,188
  • 4
  • 41
  • 49