我正在创建聊天框,该聊天框是在点击联系人时动态生成的。
在动态生成的 HTML 中,我有一个 textarea 来输入一些文本,这里是 HTML

<div class="chatboxinput">
  <textarea id="chatboxtextareaankur" class="chatboxtextarea" keydown.delegate="checkChatBoxInputKey($event, 'id', 'name')"></textarea>
</div>

但是方法“checkChatBoxInputKey”没有在任何按键按下事件上执行。
请让我知道如何解决这个问题。

最佳答案

它不起作用,因为 Aurelia 的 View 编译器没有机会编译动态生成的标记来查找绑定(bind)等。

使用 if 绑定(bind)从 DOM 添加/删除元素。下面是一个例子:

http://plnkr.co/edit/kBUz94?p=preview

<template>
  <button click.delegate="showChatBox = true">Show Chat Box</button>

  <div if.bind="showChatBox" class="chatboxinput">
    <textarea id="chatboxtextareaankur" class="chatboxtextarea" keydown.delegate="checkChatBoxInputKey($event)"></textarea>
  </div>
</template>

export class App {
  checkChatBoxInputKey(e) {
    console.log(e.which);
    return true;
  }
}

关于javascript - 绑定(bind)在动态生成的 HTML 中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33947048/

10-14 15:21