tjhack
tjhack

Reputation: 1022

how to redirect with angularjs

I have server side code which sends a response back (expressjs) to state if the user is authenticated or not.

I use $http post request and get the success back if all is ok. If user is authenticated I need to redirect the user to the relevant page.

My code here currently does not work or throw an error either when using debugging tools

$http({ method: 'POST',
            url: '/login',
            data: JSON.stringify({'username' : $scope.user.username, 'password' : $scope.user.password }),
            headers: {'Content-Type': 'application/json'}}).
            success(function (data, status, headers, config) {
                    $scope.invalid = false;
                   $window.location.href = '#/admin/';
                                   }).
            error(function (data, status, headers, config) {
                console.log(status);
                $scope.list = data;
            });

            return;
        };

Any help would be great.

Upvotes: 0

Views: 188

Answers (1)

JeffryHouser
JeffryHouser

Reputation: 39408

You can use $location to redirect

$location.path( "#/admin/" );

I would have expected your $window.location to also perform the redirect, so there could be something else going on. Do you have a route for '#/admin/' set up in a routeProvider? If not the default route would execute, if specified.

Upvotes: 2

Related Questions