Ejs里面有一个html标签

时间:2018-01-29 13:34:54

标签: javascript html node.js ejs

  <input type="checkbox" 
         <% if(email.status === 1) { %> checked <% } else { %> <% } %> >

我想显示一个选中或未选中的复选框,具体取决于使用EJS的查询结果。但当然html认为第一个%&gt;是结束标记。有什么方法可以解决这个问题吗?感谢

enter image description here

1 个答案:

答案 0 :(得分:0)

以下显示页面。我的猜测是你没有通过导致服务器错误的email.status变量。或者您正在尝试使用浏览器呈现ejs页面,这将无法使用视图引擎。

<强> checkbox.ejs

<input type="checkbox" <% if(email.status === 1) { %> checked <% } else { %> <% } %> >

<强> app.js

var express = require('express');
var bodyParser = require('body-parser'); 
var app = express();

app.use(bodyParser.urlencoded({'extended': 'true'})); 
// set the view engine to ejs
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));


app.get('/', function(req, res) {
    res.render("pages/checkbox", {email: {status: 1}});
});

app.listen(8000);
console.log('Server is running on port 8000');
console.log('http://localhost:8000/');
  

节点app.js