javascript - 如何正确评估 EJS IF/ELSE
问题描述
我的变量 'answer' 作为字符串 'success' 返回,并正确打印到视图中。
但是 IF/ELSE 语句总是对 ELSE 进行评估并显示“否”而不是“是”。我究竟做错了什么?谢谢!
<h3><%= answer %> </h3>
<% if (answer == 'success') { %>
<p>yes</p>
<% } else { %>
<p> no</p>
<% } %>
解决方案
您确定正确发送成功的价值吗?这是我所做的并得到了预期的yes
.
index.js
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render("test", {answer: 'success'});
});
app.listen(port, () => {
console.log(`listening on port ${ port }`);
});
测试.ejs
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h3><%= answer %> </h3>
<% if (answer == 'success') { %>
<p>yes</p>
<% } else { %>
<p> no</p>
<% } %>
</body>
</html>
和结果
推荐阅读
- python - Pandas 节省时间,仅在其他两个(日期)列之间的列中保留(日期)值
- reactjs - 将对象推送到数组数组 - react-redux
- scala - Spark:合并同一数据帧的列而不创建额外的数据帧
- javascript - chartjs 保持点位置,将图像与标签放在一起
- reactjs - React 功能组件中 websocket 建立代码放在哪里
- microsoft-graph-api - 使用 Graph API 在 Teams 上创建会议邀请时如何附加文件
- excel - 根据当前月份获取单元格值
- javascript - 删除节点后未删除 D3.js 链接
- html - 使用图像绝对位置时使整个选择可点击
- javascript - NodeJS:无效的数组长度分配失败 - JavaScript 堆内存不足