Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$location.path is not working

Tags:

angularjs

When user has enter correct username and password I want redirect to another location. But when I used $location.path('dashboard') here then URL of browser is changed but that page not loaded.when refresh I page using ctrl+R or click on refresh icon of browser then appropriate page is loaded.

$http.post('/login', $scope.userInfo)
        .success(function (data) {
            //Check if the authentication was a success or a failure
            //Successful POST here does not indicate success of authentication
            if (data.status === "authentication success") {

                //Proceed to load the dashboard for the user                    
                $location.path('/dashboard');

            } else if (data.status === "authentication error") {
                //Inform user of the error
                $scope.errorMessage = data.error;
                $scope.showErrorMessage = true;
            }

        })
        .error(function (error) {
            $scope.errorMessage = "Error while attempting to authenticate. Check  log.";
            $scope.showErrorMessage = true;

        });
    };

}]);
like image 781
Shankar Kamble Avatar asked Aug 09 '13 08:08

Shankar Kamble


People also ask

What is HTML5 mode?

In HTML5 mode, the $location service getters and setters interact with the browser URL address through the HTML5 history API. This allows for use of regular URL path and search segments, instead of their hashbang equivalents.

How to update the URL in AngularJS?

If the user is maximizing it the first time, share the url to this maximized view with the maximizedWidgetId on the UI. As long as you use $location(which is just a wrapper over native location object) to update the path it will refresh the view. Not only $location will refresh the view.

What is $location in AngularJS?

Overview. The $location service parses the URL in the browser address bar (based on the window. location) and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to $location are reflected into the browser address bar.


2 Answers

Have you tried using $scope.$apply after that? eg.:

if(!$scope.$$phase) $scope.$apply()

This short method comes in handy quite often as well:

https://github.com/yearofmoo/AngularJS-Scope.SafeApply

like image 134
Rafal Pastuszak Avatar answered Sep 29 '22 18:09

Rafal Pastuszak


To quote https://docs.angularjs.org/guide/$location

"The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API, $window.location.href."

So, for example, instead of using: $location.path("/path/to/something");

Use this: $window.location.href = "/path/to/something";

$window.location.href will change the URL AND load the new page.

Hope that helps.

like image 30
DondeEstaMiCulo Avatar answered Sep 29 '22 18:09

DondeEstaMiCulo