2

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

2 Answers 2

2

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');
   }
});
Sign up to request clarification or add additional context in comments.

Comments

1

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

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.