【问题标题】:MeteorJS how can I put buttons in a {{#each}} block that corresponds to the thing inside the #each?MeteorJS 如何将按钮放在与#each 内的事物相对应的 {{#each}} 块中?
【发布时间】:2015-10-30 01:22:00
【问题描述】:

抱歉,标题如此混乱,但我有好友请求列表,我想在它旁边放一个按钮来接受好友请求。问题是,显然请求数量不是固定的,所以我需要在 foreach 中进行。但是我怎样才能使按钮与我在它旁边显示的用户名相对应呢?

我有这个 html

Pending Friend Requests:
    <br><br>
    {{#each getRequests}}
        {{requester}}
    {{/each}}

我希望在请求者旁边生成一个按钮,我可以使用 JS 后端来接受请求。

【问题讨论】:

    标签: javascript html meteor spacebars


    【解决方案1】:

    试试这样的:

    {{#each getRequests}}
      <p>Requested by: {{requester}}</p>
      <button class="accept">accept</button>
    {{/each}}
    
    Template.myTemplate.events({
      'click .accept': function() {
        // do something useful here with requester?
        console.log(this.requester);
      }
    });
    

    关键的见解是#each 改变了模板块相关事件的上下文。因此,您的事件处理程序可以访问 requester 以及在请求中定义的任何其他内容。

    【讨论】:

    • 哦,这很有意义,位于 each 内部的按钮也会知道它位于 each 块中。谢谢!不知道为什么我没有想到哈哈
    猜你喜欢
    • 2018-06-03
    • 2020-07-05
    • 2011-01-20
    • 1970-01-01
    • 2017-03-28
    • 2015-11-12
    • 1970-01-01
    • 2011-03-18
    • 2013-07-18
    相关资源
    最近更新 更多