不确定我在这里做错了什么..
问题.js
questions = [];
questions.AA = 'First'
questions.BB = 'Second'
questions.CC = 'Third'
res.render('questions', { title: questions[CC], questions: questions });
问题.jade
extends layout
block content
h1= title
p #{questions.CC}
each question in questions
p= question
渲染
<body>
<h1>Third</h1>
<p>Third</p>
</body>
所以
each question in questions
p= question
似乎没有按照我的预期工作。我缺少什么?
最佳答案
您创建了一个数组,然后将值存储到字母索引而不是整数索引中。因此,each
不会循环遍历它们。您可能想像这样定义问题
:
questions = []
questions[0] = 'First'
questions[1] = 'Second'
questions[2] = 'Third'
或者,更惯用的说法是:
questions = [
'First',
'Second',
'Third'
]
您必须想出一些办法来替换您获取标题
的方式,但这应该可以修复循环。
关于javascript - 将数组从node.js/express传递到jade模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27098251/