user2997960
user2997960

Reputation: 23

nodejs expressjs angularjs routing

i want to redirect from an express function to an angularjs partial to load with the controller of that view.

app.js -->nodejs

function(req, res, next) {
    console.log(req.session);
    if(req.session.user/* && req.session.user.role === role*/)
      next();
    else
      res.redirect("/#/login");
  }

app.js -->angularjs

app.config(function($routeProvider){
$routeProvider
.when("/create", {
    templateUrl : "/users/usersCreate",
    controller : "users"
})
.when("/delete", {
    templateUrl : "/users/usersDelete",
    controller : "users"
})
.when("/login", {
    templateUrl : "/sessions/sessionsCreate",
    controller : "sessionsCtr"
})

.otherwise({ reditrectTo : "/" });

})

its not working :( help

Upvotes: 2

Views: 4433

Answers (2)

timsmiths
timsmiths

Reputation: 167

Depending on how you have used ng-view within your app, you could use node to render and send partials conditionally.

Angular -> routing

 app.config(function($routeProvider){
    $routeProvider
    .when("/create", {
        templateUrl : "/api/v1/partial/partialName",
        controller : "CtrlOne"
    })
    .when("/delete", {
        templateUrl : "/api/v1/partial/partialTwoName",
        controller : "CtrlTwo"
    })
    .otherwise({ reditrectTo : "/" });

Node

app.get('/api/v1/partials/:partial', function (req, res){
   var partial = req.params.partial
   if(req.session.auth){
      res.render('views/partials/'+partial+'.jade');
   } else {
      res.render('views/partials/login.jade');
   }
});

Upvotes: 2

Hector Correa
Hector Correa

Reputation: 26690

You cannot redirect Angular.js when requesting a partial. Angular.js is issuing an AJAX call and that won't follow a redirect response in the same way a browser does.

This other answer provides guidance on how to go about it:

https://stackoverflow.com/a/15261558/446681

Upvotes: 1

Related Questions