2

When a user clicks a category on the shop-landing page, a sessionStorage var is set and you are redirected to the shop page but the redirect is funky

***OLD CODE (works but breaks on iOS 7.1.1, i think it's a dirty hack. It redirects to root/shop)

.controller('myController', function ($scope) {
    $scope.sortCategory = function sortCategory(category) {
        sessionStorage.setItem('sortCategory', '.' + category);
        window.location.assign("/shop");
    };
})

***NEW CODE (doesn't work, redirects to root/shop-landing#/shop)

.controller('myController', function ($scope, $location) {
    $scope.sortCategory = function sortCategory(category) {
        sessionStorage.setItem('sortCategory', '.' + category);
        $location.path("/shop");
    };
})

The desired url is root/shop.

What is the proper way to do this?

9
  • 1
    What router are you using? ng-route or ui-router? Commented Dec 16, 2014 at 18:51
  • The module is using ngRoute Commented Dec 16, 2014 at 18:53
  • Can you share the router code? Commented Dec 16, 2014 at 18:55
  • Perhaps the old code breaks because of it's use of 'window' instead '$window'. Where do i find the router code? Commented Dec 16, 2014 at 18:57
  • 1
    window.location.href = url is the standard way of redirecting to a URL in javascript. The angular $location service deals only with hash routing Commented Dec 16, 2014 at 19:59

2 Answers 2

1

Can you not just use window.location.href = '[enter_url_here]'?

This is pretty standard for redirecting a user with JavaScript.

Sign up to request clarification or add additional context in comments.

2 Comments

I was just about to answer with Alex Hills suggestion. This gives me the required URL, but have to wait until it is tested on an iOS 7.1.1 device to see if it fixed the bug 'window.location.assign()' was causing.
Buttons still unresponsive on iOS 7.1.1.
0

You have two possibilities. The first one is to use the mentioned property:

window.location.href = '/shop';

or the $location service

$location.path('/shop');

Here on plunkr is a working example. And probably you may use the absolute navigation in your path like this:

window.location.href = '../shop';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.