batman
batman

Reputation: 3685

$location is not working in Angular

I have an rest api, in which the api is sending instruction to redirect (301 is being sent).

And I have my angular js code like this:

    var request = $http.post(LOGIN_URL,{username:'tes',password:'test'})

    request.success(function(html)
    {
        if(html.failure) {
            console.log("failure")
            $scope.errorMessage = html.failure.message
        }

        else {
            console.log("Success here....")
            $location.path("route")
        }
    })

I can see in the browser log that it is coming in the else part ( Success here..... is being printed). But the url is not changed. $location.path doesnt do anything; I have also tried $location.url which also results the same thing.

And also I'm injecting the $location to my controller.

Where I'm making mistake?

Thanks in advance.

Upvotes: 0

Views: 59

Answers (2)

guilhebl
guilhebl

Reputation: 8990

Also make sure you have a corresponding view with that name and when you're registering your controller you have the '$location' var being injected in the declaration of your controller like this example:

    controllers.controller('MyCtrl', ['$scope', '$route', '$location', 'MYService',
        function($scope, $route, $location, MyService) {

// ... controller code

    }])

also you might want to debug your route changing to see what is happening with a location change listener, like in this example:

return app.config(['$routeProvider', function($routeProvider) {

        $routeProvider.    
          when('/home', {
            templateUrl: 'partials/home.html',
            controller: 'MyCtrl'
          }).
          when('/login', {
            templateUrl: 'partials/login.html',
            controller: 'LoginCtrl'
          }).
          when('/error/:errorId', {
              templateUrl: 'partials/error.html',
              controller: 'ErrorCtrl'
          }).      
          otherwise({
            redirectTo: '/home'
          });

    }]).run(function($rootScope, $injector, $location) {
        $rootScope.$on("$locationChangeStart", function(event, next, current) {

            console.log('current=' + current.toString());
            console.log('next=' + next.toString());         
        });
    });
});

Upvotes: 1

TGH
TGH

Reputation: 39248

Try something like this

$location.path("/myroute")

You have to have a ng-view on the page as well.

Upvotes: 1

Related Questions