Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Angular to send request to server on HTML5 mode?

I m tring to build and web using angular and nodejs. I'm loading Angular on /home path where / contains login and registration form. Here is my angular configuration:

window.app.config(['$routeProvider', '$locationProvider',
    function($routeProvider,$locationProvider) {
        $locationProvider.html5Mode(true);
        $locationProvider.hashPrefix('!');
        $routeProvider.
        when('/profile', {
            templateUrl: '/views/account.html',

        }).
        when('/edit', {
            templateUrl: '/views/edit.html',

        }).
        when('/home', {
            templateUrl: 'views/index.html'
        }).
        when('/signout', {
            templateUrl: 'views/signout.html'
            //on this view i load a controller which submits a form to /signout
        }).

        otherwise({
            redirectTo: '/home'
        });
    }
]);

on server side routing:

    app.get('/',function(){
          res.render('index',{
             user: req.user? req.user:'guest'
        });
    });
    app.post('/login',function(){
             //if success redirect to /home
             //if fails redirect to /
    });
    app.get('/auth/facebook', passport.authenticate('facebook', { scope: [ 'email',                  'user_about_me', 'read_stream', 'publish_actions'], failureRedirect: '/' }));
  app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/' }), users.authCallback);
    app.get('/signout',function(){
             //signing out the user here...and redirects to /
    });
    app.get('/home',function(req,res){
        res.render('users/home',{
          user: req.user? req.user:'guest',
          message: req.flash('error')

        })
      }); 
    app.get('/profile',function(req,res){
        res.render('users/home',{
          user: req.user? req.user: 'guest',
          message: req.flash('error')

        })
      });
      app.get('/edit',function(req,res){
        res.render('users/home',{
          user: req.user? req.user:'guest',
          message: req.flash('error')

        })
      });

How can I send request by clicking on a link on url /home to go /auth/facebook due to Angular routing if I click on facebook auth it sends me to /home. Angular preventing from sending request to server, it's just go to url /home, I tried removing otherwise /home by replacing /. The result is same just no view is loaded there.

like image 691
biborno Avatar asked Nov 08 '13 11:11

biborno


2 Answers

From the Docs:

In cases like the following, links are not rewritten; instead, the browser will perform a full page reload to the original link.

  • Links that contain target element. Example: <a href="/ext/link?a=b" target="_self">link</a>

  • Absolute links that go to a different domain. Example: <a href="http://angularjs.org/">link</a>

  • Links starting with '/' that lead to a different base path when base is defined. Example: <a href="/not-my-base/link">link</a>

So, as a simplest solution, just add target="_self" to your link.

like image 54
Stewie Avatar answered Nov 12 '22 02:11

Stewie


If you'd rather change the route configuration in one place instead of changing all link elements, you can do this:

$routeProvider.when('/externalpath', { resolve: {
    redirect: ['$location', function($location) {
        window.location.replace($location.absUrl());
    }]
}});

This basically invokes the inlined function whenever a link to that route is hit, and the function just redirects the browser to the target location (outside of angular). The 'resolve' configuration is processed regardless of whether a template or controller is configured for the route, and the 'redirect' name is basically just a dummy name - the important thing is that the function gets executed to get its value (which is unused in this case).

This is also useful when e.g. you have a /signout url that needs to reach the server. You can also use an otherwise() configuration instead of when() if you want all non-configured paths to be handled by the server.

like image 3
amichair Avatar answered Nov 12 '22 02:11

amichair