angularjs - Nodejs Express 渲染不适用于 ejs

标签 angularjs node.js express ejs

我在我的 Node 服务器上使用 express ejs 在多 View 应用程序中呈现 View 。在客户端,我使用的是 Angularjs。初始 View 是登录页面,使用 res.render('pages/login') 时可以正确显示。我想在用户成功连接时呈现一个新 View ,但对于下一个 res.render('pages/home'),它停留在登录页面上,尽管我在客户端接收到正确的 HTML边。 这是我的相关代码:

partial file structure :

   App
     public/js/Script.js
     views/pages
          login.ejs
          home.ejs
     server.js

server.js

...
app.get('/',function(req, res){
   res.render('pages/login'); //being displayed
});

app.get('/rest/home',function(req, res){
   res.render('pages/home'); //not being displayed
});
app.post('/login',function(req, res){
   //authentification
});
...

Script.js

.controller ('loginController', function ($scope, $http) {     
   $scope.authent = function(){
      $http({
         method: 'POST',
         url: '/login',
         data: $.param($scope.loginData),
         headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
      }).success(function(res){
         $scope.launch();
      });
   $scope.launch = function(){
      $http({
         method: 'GET',
         url: '/rest/home',
      }).success(function(res){
         alert(res); //Here, I am getting the content of home.ejs, but the view doesn't load in browser
      });     
   };      
})
.controller ('homeController', function () {
});

login.ejs

<html ng-app="App">
<head>
       ...
</head>
<body  ng-controller="loginController">
    <div id="loginView">
       <form name="login" id="login" ng-submit="authent()">
          <input id="login_id" name="login_id" type="text" ng-model="loginData.loginId"/> 
          <input id="password" name="password" type="password" ng-model="loginData.password"/>
          <input type="submit" class="submit" value="Login"/>
       </form>
    </div>
</body>
</html>

home.ejs

<html ng-app="App">
<head>
   ...
</head>
<body  ng-controller="homeController">
   <h1>Home</h1>
</body>
</html>

这是我第一次将 ejs 与 angularjs 一起使用,我不太确定我的方法,因此也欢迎任何关于更好方法的建议。我试过使用 Route,但不是很满意。 我希望我的解释清楚。

最佳答案

最后我使用了$routeProvider。这是解决方案:

文件结构:

App
  /public/js/Script.js
  /views/
       layout.ejs
       /partials/
          login.ejs
          home.ejs
  /routes/
       index.js
  server.js

服务器.js

...
var engine = require('ejs-locals');
var routes = require('./routes');
app.engine('ejs', engine);

app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');    
app.get('/partials/:name', routes.partials);
app.get('/', routes.index); //Important to place this last
...

索引.js

exports.index = function(req, res){
   res.render('layout');
};

exports.partials = function (req, res) {
   var name = req.params.name;
   res.render('partials/' + name);
};

脚本.js

app.config(function ($routeProvider, $locationProvider) {
   $locationProvider.html5Mode(true);
   $routeProvider
      .when('/', {
         templateUrl : 'partials/login',
         controller  : 'loginController'
      })
      .when('/home', {
         templateUrl : 'partials/home',
         controller  : 'homeController'
      })
      .otherwise({redirectTo: "/"});
})
app.controller ('loginController', function ($scope, $http) {     
  ...
   $scope.launch = function(){
      $http({
         ...
      }).success(function(res){
         $location.path('/home');  
      });     
   };      
})

布局.ejs

...
<body>
   ...
   <div ng-view></div> <!-- This section will be replaced with rendered view -->
   ...
</body>
...

关于angularjs - Nodejs Express 渲染不适用于 ejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29892065/

相关文章:

javascript - 如何使 AngularJS 中的表单无效? (或者如何禁用 angularjs 中的表单?)

android - 如何使用 ClientID 和 ClientSecret 在 Phonegap 中使用 Angularjs 登录 Google OAuth2

javascript - 如何在下一页中选择相同的选择选项

node.js - 在 Express Node 应用程序中使用 connect-livereload

javascript - 快速处理空的 req.body?

javascript - 使用 $resource 添加新项目

node.js - Express.js : show toastr message

node.js - 在 Mongoose 中填充后查询

javascript - Then is not a function promise 错误

node.js - Express 中间件 - 添加 promise 拒绝处理程序