【问题标题】:Node js and ejs using loop使用循环的节点 js 和 ejs
【发布时间】:2018-10-31 21:50:30
【问题描述】:

我是 node.js 的新手,现在我遇到了 EJS 模板的问题。我注意到我的 for 循环没有通过 EJS 模板运行。我试图制作一个非常基本的待办事项应用程序。

这是我制作的项目的层次结构

This is my project hierarchy

这是我的 App js 模块

let express = require('express');
let todoController = require('./Controller/todoController')
let app = express()
app.set('view engine', 'ejs');
todoController(app);




app.listen(3000,function(){
console.log('server started on http://localhost:3000');
})

todoController.js

 let toDoList = ['Go to university','Smoking sigrate'];
    module.exports= function(app){
        app.get('/', function(req, res){
            res.render('index.ejs', {toDoList: toDoList});
        });

        app.get ("*", function(req,res){
            res.send("<h1>Invalid page</h1>");
        })
    }

index.ejs

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Todo</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
    crossorigin="anonymous">

</head>

<body>


  <!-- As a heading -->
  <nav class="navbar navbar-light bg-dark">
    <span class="navbar-brand mb-0 h1 text-white">Todo List</span>
  </nav>
  <br><br>
  <div class="container">
    <form>
      <div class="form-group text-white bg-dark">
        <label for="formGroupExampleInput2">Enter to do item</label>
        <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Input a item to do list....">
        <input type="submit" class="form-control bg-primary text-white h3" >
      </div>
    </form>
  </div>

  <br><br>
    <div class="row">
    <ul class="col-6 mx-auto" >

      <% for(let i =0; toDoList.length; i++){ %>
        <li> <%= toDoList[i] %> </li>
      <% } %>

    </ul>
  </div>

</body>

</html>

【问题讨论】:

    标签: javascript node.js loops express ejs


    【解决方案1】:

    你错过了循环中的条件

          <% for(let i =0;i < toDoList.length; i++){ %>
            <li> <%= toDoList[i] %> </li>
          <% } %>
    

    【讨论】:

      【解决方案2】:
      <% for(let i =0; i<toDoList.length; i++){ %>
          <li> <%= toDoList[i] %> </li>
        <% } %>
      

      使用 i

      【讨论】:

        猜你喜欢
        • 2018-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-29
        • 1970-01-01
        • 1970-01-01
        • 2021-07-30
        相关资源
        最近更新 更多