简体   繁体   English

nodejs expressjs angularjs路由

[英]nodejs expressjs angularjs routing

i want to redirect from an express function to an angularjs partial to load with the controller of that view. 我想从一个表达函数重定向到angularjs局部加载该视图的控制器。

app.js -->nodejs 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.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 它不起作用:(帮助

Depending on how you have used ng-view within your app, you could use node to render and send partials conditionally. 根据您在应用程序中使用ng-view的方式,可以使用node来有条件地渲染和发送部分。

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');
   }
});

You cannot redirect Angular.js when requesting a partial. 请求局部时,您不能重定向Angular.js。 Angular.js is issuing an AJAX call and that won't follow a redirect response in the same way a browser does. Angular.js正在发出AJAX调用,该调用不会像浏览器那样跟随重定向响应。

This other answer provides guidance on how to go about it: 这个其他答案提供了有关如何进行的指导:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM