V
V
Vadim2015-05-13 01:26:53
JavaScript
Vadim, 2015-05-13 01:26:53

How to change url via "otherwise" in angular+node without page reload?

On the client side:

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

    $routeProvider
        .when('/', {
            templateUrl: 'components/home.html',
            controller: 'homeController'
        })
        .otherwise('/');

    $locationProvider.html5Mode(true);
}]);

on the server:
app.get('*', function(req, res, next) {
      res.redirect('/#' + req.originalUrl);
});

But the page reloads if you try to apply, for example, the URL "/qwerty". Is there a way to change bad url without reload?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kano, 2015-05-13
@Kano

You need to subscribe to the link change event by the $location service and stop the execution of the event, like this

$scope.$on('$locationChangeStart', function(event) {
       event.preventDefault();
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question