javascript - angular: pass data when doing a $location.path() - Stack Overflow

admin2025-04-20  0

I have two views right now.

  • login
  • main

Right now I login and change my path to /main which works fine. When I am not logged in, and try to visit /main my web service returns "Access denied for user anonymous" which I then forward them to / which is my login view. How can I pass something so my LoginController knows they were forwarded from /main to alert them to login first?

LoginController.js

VforumJS.controller('LoginController', function($scope, $location, $routeParams, LoginModel)
{
    $scope.email        = "";
    $scope.password     = "";
    $scope.fetching     = false;
    $scope.error        = null;

    $scope.login = function()
    {
        $scope.error    = null;
        $scope.fetching = true;
        LoginModel.login($scope.email, $scope.password);
    }

    $scope.$on('LoginComplete', function(event, args)
    {
        log('login plete: ' + args.result);
        $scope.fetching = false;
        if (args.result == "success")
        {
            $location.path('/main');
        }
        else
        {
            $scope.error = args.result;
        }
    });
});

MainController.js

VforumJS.controller('MainController', function($scope, $location, $routeParams, MainModel)
{
    $scope.currentTitle     = '-1';
    $scope.presentationData = MainModel.getPresentations();

    $scope.$on('PresentationsLoaded', function(event, args)
    {
        log(args.result);
        if (args.result != "Access denied for user anonymous")
        {
            //-- Parse preso data
            $scope.presentationData = args.result;
        }
        else
        {
            //-- Need to login first, route them back to login screen
            $location.path("/");
        }
    });
});

I have two views right now.

  • login
  • main

Right now I login and change my path to /main which works fine. When I am not logged in, and try to visit /main my web service returns "Access denied for user anonymous" which I then forward them to / which is my login view. How can I pass something so my LoginController knows they were forwarded from /main to alert them to login first?

LoginController.js

VforumJS.controller('LoginController', function($scope, $location, $routeParams, LoginModel)
{
    $scope.email        = "";
    $scope.password     = "";
    $scope.fetching     = false;
    $scope.error        = null;

    $scope.login = function()
    {
        $scope.error    = null;
        $scope.fetching = true;
        LoginModel.login($scope.email, $scope.password);
    }

    $scope.$on('LoginComplete', function(event, args)
    {
        log('login plete: ' + args.result);
        $scope.fetching = false;
        if (args.result == "success")
        {
            $location.path('/main');
        }
        else
        {
            $scope.error = args.result;
        }
    });
});

MainController.js

VforumJS.controller('MainController', function($scope, $location, $routeParams, MainModel)
{
    $scope.currentTitle     = '-1';
    $scope.presentationData = MainModel.getPresentations();

    $scope.$on('PresentationsLoaded', function(event, args)
    {
        log(args.result);
        if (args.result != "Access denied for user anonymous")
        {
            //-- Parse preso data
            $scope.presentationData = args.result;
        }
        else
        {
            //-- Need to login first, route them back to login screen
            $location.path("/");
        }
    });
});
Share Improve this question asked Jan 8, 2014 at 19:45 RonnieRonnie 11.2k22 gold badges84 silver badges142 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You can use $location.search() in your MainController to pass query string to the LoginController.

Inside you MainController:

    if (args.result != "Access denied for user anonymous")
    {
        //-- Parse preso data
        $scope.presentationData = args.result;
    }
    else
    {
        //-- Need to login first, route them back to login screen
        $location.search({ redirectFrom: $location.path() });
        $location.path("/");
    }

And then in your LoginController, shortened for brevity:

VforumJS.controller('LoginController', function($scope, $location, $routeParams, LoginModel)
{
    var queryString = $location.search();

    $scope.$on('LoginComplete', function(event, args)
    {
        log('login plete: ' + args.result);
        $scope.fetching = false;
        if (args.result == "success")
        {
            if (queryString && queryString.redirectFrom) {
                $location.path(queryString.redirectFrom);
            } else {
                $location.path('/somedefaultlocation');
            }
        }
        else
        {
            $scope.error = args.result;
        }
    });
});

Alternatively you can use a shared service, maybe even your LoginModel to set a parameter from MainController to indicate the redirect came from it.

Update Even better still, use $httpProvider.interceptors to register a response interceptor, and then use the same $location.search() technique described above to redirect to the login screen on authentication failure. This method is ideal as your controllers are then clean of authentication logic.

$location broadcasts $locationChangeStart and $locationChangeSuccess events, and the third param of each is oldUrl.

One solution would be to have a service that subscribes to $locationChangeStart in order to save the current and old urls.

When you hit /, your LoginController can check your service to see if the oldUrl is /main, and then act accordingly.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745110547a285556.html

最新回复(0)