【问题标题】:Transfer value from Node JS to Jade将价值从 Node JS 转移到 Jade
【发布时间】:2016-08-02 11:26:45
【问题描述】:

我正在尝试将参数从节点 js 传输到我的 Jade 页面中的输入标签, 这是我的代码:

节点js:

res.render("index" ,{title: userName});

翡翠页(称为“索引”):

.input
    input(type='text', value= #{title} ,  maxlength='15')

我在这里做错了什么?

这是我的错误信息:

48|输入(type='text', value= #{title} , maxlength='15')

意外令牌非法 at 函数(原生)

`

【问题讨论】:

    标签: javascript node.js express pug


    【解决方案1】:

    它会按原样工作吗:value=title

    .input
        input(type='text', value=title ,  maxlength='15')
    

    【讨论】:

      【解决方案2】:

      您正在使用如下所述的转义字符串插值:http://jade-lang.com/reference/interpolation/ 这不能用作属性,您必须使用缓冲代码,如下所述:http://jade-lang.com/reference/code/

      看起来像这样:

      // escaped code
      .input
          input(type='text', value= title ,  maxlength='15')
      // unescaped code
      .input
          input(type='text', value!= title ,  maxlength='15')
      

      如果你想为你的属性使用一个对象,你可以用input&attributes(object)来处理它,就像这里描述的:http://jade-lang.com/reference/attributes/#and-attributes

      Node.js

      res.render("index" ,{
         input: {
            type: 'text',
            title: userName, 
            maxlength: '15',
            name: 'myInputName'
         }
      });
      

      玉:

      .input
          input&attributes(input)
      

      两者的结果:

      <div class="input">
         <input type="text" value="username" maxlength="15" name="myInputName" />
      </div>
      

      这是 Jade 中没有 Node 的 workinh Pen: http://codepen.io/pure180/pen/oLPGJy

      【讨论】:

      • @Tal ... 那么你在 app.js 或你的节点脚本中做错了什么。我的例子很完美。这是一支专业的笔:codepen.io/pure180/pen/oLPGJy
      • 是的,我的脚本有问题。你的回答是正确的。谢谢!
      猜你喜欢
      • 1970-01-01
      • 2014-01-24
      • 2016-10-28
      • 2014-04-15
      • 2012-05-04
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      相关资源
      最近更新 更多