【问题标题】:AureliaJS - replacing looped elements with a template (passing values?)AureliaJS - 用模板替换循环元素(传递值?)
【发布时间】:2016-12-19 11:45:18
【问题描述】:

我找到了https://sean-hunter.io/2016/10/23/inter-component-communication-with-aurelia/,它解释了如何在父模板和子模板之间进行简单的值通信。

现在,我正在尝试将其应用于联系人管理器教程:

...也在https://github.com/aurelia/app-contacts/tree/master/src - 特别是循环联系人项目,但我无法让它工作。

我以 gist 为例,它可以在 gist.run 上运行(注意,gist.run 似乎只能在 Chrome 中工作,而不是在 Firefox 50 中 - 但你可以将 gist.run/?id= 替换为 gist.github.com/ 并查看在代码处):

这就是我想要做的:在原来的联系人应用程序中,src/contact-list.html 中有这个可以正常工作:

<template>
  <div class="contact-list">
    <ul class="list-group">
      <li repeat.for="contact of contacts" class="list-group-item ${contact.id === $parent.selectedId ? 'active' : ''}">
        <a route-href="route: contacts; params.bind: {id:contact.id}" click.delegate="$parent.select(contact)">
          <h4 class="list-group-item-heading">${contact.firstName} ${contact.lastName}</h4>
          <p class="list-group-item-text">${contact.email}</p>
        </a>
      </li>
    </ul>
  </div>
</template>

现在,我想用新模板替换“循环”li 的内部元素 - 即 ah4p

所以,我发了src/contact-list-item.html

<template>
  <a route-href="route: contacts; params.bind: {id:theContact.id}" click.delegate="$parent.$parent.select(theContact)">
    <h4 class="list-group-item-heading">${theContact.firstName} ${theContact.lastName}</h4>
    <p class="list-group-item-text">${theContact.email}</p>
  </a>
</template>

...和src/contact-list-item.js:

import {bindable} from 'aurelia-framework';

export class ContactListItem {
  @bindable theContact;
}

...并将src/contact-list.html 更改为:

<template>
  <require from="./contact-list-item"></require>

  <div class="contact-list">
    <ul class="list-group">
      <li repeat.for="contact of contacts" class="list-group-item ${contact.id === $parent.selectedId ? 'active' : ''}">
        <contact-list-item theContact.bind="contact"></contact-list-item>
      </li>
    </ul>
  </div>
</template>

基本上,我在contact-list-item 类中创建了一个名为theContact 的属性,我想将它绑定到contact,这是repeat.for="contact of contacts" 中的looper 变量——不幸的是,这不起作用,因为数据没有传播(因此联系人姓名为空)。此外,即使我在新模板中将click.delegate="$parent.select(contact)" 更改为click.delegate="$parent.$parent.select(theContact)",联系人字段的点击也不会传播以显示详细信息。

为了让数据从 li repeate.for 循环传播到新的替换模板,我需要做什么,并让应用对新模板的点击做出反应?

【问题讨论】:

标签: javascript aurelia


【解决方案1】:

快速回答:

contact-list.html,更改:

<contact-list-item theContact.bind="contact"></contact-list-item>

到:

<contact-list-item the-contact.bind="contact"></contact-list-item>

说明:

HTML 不区分大小写,但惯例是只使用小写字母。为了尊重这一点,Aurelia 在与您的 HTML 代码交互时将所有 camelCase 变量转换为破折号参数和元素名称。

更多信息:

Dwayne Charrington 在这里写了一篇关于这个主题的好博客:

http://ilikekillnerds.com/2016/06/dont-get-skewered-kebab-case-aurelia/

【讨论】:

  • 哦,哇——我从没想过会这样;非常感谢@LStarky!事实上,我只是将contact-list-item.htmlcontact-list-item.js 中的thecontact 替换为theContact,然后代码开始工作 - even if contact-list.html still 有@ 987654331@ 驼色箱!!甚至点击事件也有效!这是更改后的工作代码:gist.run/?id=766cdd682b215a10ad9d7913ecd8d119
  • 很高兴你能成功。重要的是要记住 JavaScript、HTML 和 Aurelia 如何处理变量、元素和属性的大小写。
猜你喜欢
  • 1970-01-01
  • 2013-02-01
  • 2016-04-25
  • 2013-10-07
  • 2019-05-06
  • 2021-05-03
  • 2021-04-02
  • 2021-02-10
  • 1970-01-01
相关资源
最近更新 更多