1

I want to redirect a user on specific page using ajax call in angularjs. I am able to redirect using below mentioned code but when i again want to redirect user to root page i am unable to do so as the value of $window.location.href+ 'getTechnicianWorkOrder/'+woId is persisting as is:

  $scope.getTechnicianWorkOrderFormURL = function(woId){ 
       return $window.location.href + 'getTechnicianWorkOrder/'+woId;   
     };

Another place where i want to redirect to root page:

 $scope.getAssignedListURL = function(){     
    return $window.location.href;
};

Note: i want to make this redirection work even in offline mode of HTML5 cache-manifest.

0

2 Answers 2

2

Using the $location service.

$location.path('/');

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

2 Comments

where to use it? Can u edit my code sample above and show where to use it?
I guess you have an ng-click on your app or some ajax callback that invokes the function to go back to the root page, in that function put the code.
0

The answer by @prashant-palikhe is the right one, $location.path('/'); is the route to your root path. just use the dependency of $location to your controller like this:

yourapp.controller('YourController', ['$location', function($location) {
    ...
} 

I always use the ui.router for my routes and in there you can add $urlRouterProvider.otherwise('/') for fallback in any unknown state or route.

In some cases you can add something like this in your states:

resolve : {
    dataObj : ['$http', function($http) {
        return $http({method : 'GET', url : '/your/ajax/endpoint'})
    ;}],
},
onEnter : ['dataObj', '$state', function(dataObj, $state) {
    // dataObj is your ajax response object. Based on this you can redirect to a certain state of needed
    $state.go('default');
}]

resolve is data that preloads data onEnter is called before entering the state. This can be used as some sort of middleware.

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.